parametrized test
One test function run many times with different data, each run reported as a separate test case.
In more detail
@pytest.mark.parametrize takes the parameter names as one string and a list of tuples, one per case. Each case can be rerun alone, and ids= gives each case a readable name for reports. Parametrize the data, not the behaviour: if the body needs an if statement to cope, you have two tests.
Example
@pytest.mark.parametrize(
"price, expected",
[(1000, 900), (999, 899)],
ids=["round", "off-by-a-penny"],
)Related terms
- tuple — An ordered, fixed-length group of values in which each position can have its own meaning and type.
- edge case — An input at the limits of what code must handle, such as an empty list, zero, a single item or the largest allowed value.
- node ID — The full address pytest gives each test, made of the file path, two colons and the test name, such as test_discount.py::test_a_silly_percentage_is_refused.
- expected failure — A test marked with @pytest.mark.xfail because it is known to fail today, reported as xfailed instead of failed.
Taught in
- Parametrised Tests — Automated Testing, TDD & Quality Engineering