Sub topic 10
Running one test over many inputs with parametrize
This chapter assumes you can write a basic pytest test with plain assert, run pytest from a terminal, and read a failure report. It uses the cart.py module from earlier chapters.
The problem: copy-pasted tests
Suppose you want to check total with an empty list, one price, and several prices. You could write three test functions that differ only in their data. That is easy to write, but every new case adds another near-duplicate function.
pytest.mark.parametrize lets you write the test body once and supply a table of inputs.
Your first parametrized test
import pytest
from cart import total
@pytest.mark.parametrize(
"prices, expected",
[
([], 0),
([5], 5),
([1, 2, 3], 6),
],
)
def test_total_sums_prices(prices, expected):
assert total(prices) == expectedThe decorator takes two arguments:
- A string of argument names, separated by commas. These must match parameters of the test function.
- A list of values. Each item is one case, and it is a tuple whose entries line up with the names.
pytest runs the function once per case and reports each run as its own test. With -v you will see output like this:
pytest -v test_cart.pyEach case gets an ID built from its values, such as test_total_sums_prices[prices0-0], test_total_sums_prices[prices1-5], and test_total_sums_prices[prices2-expected2]. If one case fails, the others still run, and the report names the failing case.
Readable case IDs
Auto-generated IDs like prices0-0 are hard to read. Use pytest.param with an id to name cases:
import pytest
from cart import total
@pytest.mark.parametrize(
"prices, tax_rate, expected",
[
pytest.param([], 0.0, 0, id="empty-cart"),
pytest.param([10, 20], 0.0, 30, id="no-tax"),
pytest.param([10, 20], 0.1, 33.0, id="ten-percent-tax"),
],
)
def test_total_with_tax(prices, tax_rate, expected):
assert total(prices, tax_rate) == pytest.approx(expected)A failure now reads test_total_with_tax[ten-percent-tax], which tells you the scenario without opening the code. The test uses pytest.approx because 30 * 1.1 is a floating-point result.
Selecting a single case
Use -k to match part of a test name, or give the full ID:
pytest -k "ten-percent-tax"
pytest "test_cart.py::test_total_with_tax[no-tax]"Common errors
- Name mismatch. If a name in the string is not a parameter of the function, pytest fails at collection with an error saying the function uses no argument with that name.
- Wrong tuple length. Each case must have exactly as many values as there are names.
- Mixing concerns. If a case needs different assertions, not just different data, write a separate test.
- Mutable values. Lists in the table are shared objects. If the test mutates
prices, the change can leak into later uses of the same object, so avoid mutating parameters.
Exercise
Exercise
Write a parametrized test for total that checks these cases with tax_rate=0.5: [] gives 0, [10] gives 15, and [2, 2] gives 6. Give each case an id. What does pytest report if the [10] case fails?
Show answer
Example:
import pytest
from cart import total
@pytest.mark.parametrize(
"prices, expected",
[
pytest.param([], 0, id="empty"),
pytest.param([10], 15, id="single"),
pytest.param([2, 2], 6, id="pair"),
],
)
def testtotalhalftax(prices, expected):
assert total(prices, taxrate=0.5) == pytest.approx(expected)
pytest reports the failure for test_total_half_tax[single] only. The empty and pair cases still run and are reported on their own.