Parametrize one pytest test function with multiple inputs
Run one test function with several input sets using @pytest.mark.parametrize, keeping results separate per case.
13 Jun 2026, 00:59 UTC

Problem and useful answer
You need to check the same behavior with several inputs—for example, verifying that a discount function handles 10 percent, 25 percent, and zero percent correctly. Writing a separate test function for each input duplicates setup and makes failures harder to compare.
The useful answer is to decorate one test function with @pytest.mark.parametrize. pytest expands the function into one test call per argument set. Each invocation runs with its own setup and teardown by default, appears as a separate node in verbose output, and fails independently of the others.
Worked configuration
In this example, a module named pricing.py provides apply_discount(price, percent). The test file below defines four cases in one function.
# tests/test_discount.py
import pytest
from pricing import apply_discount
@pytest.mark.parametrize(
"price,percent,expected",
[
(100, 10, 90),
(50, 0, 50),
(200, 25, 150),
(0, 50, 0),
],
ids=["ten-percent", "no-discount", "quarter-off", "free-item"],
)
def test_apply_discount(price, percent, expected):
assert apply_discount(price, percent) == expected
The first decorator argument names the test parameters as a comma-separated string. The second argument is a list of tuples; each tuple supplies one case. The optional ids list gives each case a readable label instead of relying on positional values.
When pytest collects this file, it creates four test nodes from the single function. A failure in one case does not stop the other cases from running.
Commands and checks
Run these from the project root that contains the tests/ directory. No elevated permissions are required; the user only needs read access to the test files.
pytest -v: confirms each parameter case appears as a separate node, combining the function name and the id label in brackets.pytest --collect-only -q: lists generated cases without executing them. Use this to verify the count and labels before a long suite runs.pytest --version: record the pinned pytest version in CI. Consult the changelog for your pinned version before upgrading.
Risk: because collection happens before execution, a syntax error in the decorator or its argument list prevents the whole module from being collected, so none of its cases run.
Limits and common mistakes
Multiple decorators multiply cases
Stacking two @pytest.mark.parametrize decorators produces the Cartesian product of their values. Three values on one decorator and four on another yield twelve test invocations. Large combinations slow collection and make reports hard to read. Limit combinations or split them into separate files.
Shared mutable state between cases
Do not use a module-level list or global variable to accumulate data across parametrized cases. Function-scoped fixtures run once per invocation, but globals persist across all cases unless you explicitly reset them. Pass data through parameters and fixtures instead.
One failure does not abort the rest
By default, every parameter case runs even if an earlier case fails. Use pytest -x or --exitfirst if you want the run to stop at the first failure.
Indirect fixture resolution
Set indirect=True when a parameter value should be passed to a fixture with the same name rather than injected directly into the test function. This is useful for setup that depends on the case, but it adds indirection; verify fixture names match the parameter names exactly.
Practical verification
To confirm failure isolation, temporarily change one case to assert False, run pytest -v, and confirm the report marks only that node as failed while the other parameter cases still pass. Revert the change before committing.
To verify case count without running tests, use pytest --collect-only -q and compare the listed nodes against the number of tuples in your parametrize list.
Diagram labels
- Decorator argument names:
price,percent,expected - Argument tuples: one row per case
- Generated node IDs: function name plus bracketed id label
- Setup and teardown: run once per invocation