To run many inputs through one test with pytest parametrize, decorate the test with @pytest.mark.parametrize, give it the argument names and a list of value sets, and pytest runs the function once per set. Each set is reported as its own test, so one failing input never hides the others.
This guide uses pytest 8 on Python 3.13. It assumes you can already write and run a basic pytest test.
Imagine you test a function that converts a string to a slug. Without parametrization you either write one test per input, which repeats the same code, or you loop over inputs inside a single test, which stops at the first failure and tells you little about the rest.
Parametrization gives you one test body and a table of cases. pytest generates a separate test item for each row, so the report shows exactly which inputs passed and which failed.
Here is the function used throughout this guide:
The decorator takes two main arguments: a string of comma-separated argument names, and an iterable of value sets. Every name must match a parameter of the test function.
Run it with pytest -v:
Five rows produce five tests. Each is selected, rerun and reported on its own. You can pass a list of tuples, as above, or a single argument name with a plain list of values.
The automatic IDs, such as Hello World-hello-world, get hard to read when values are long or empty. Wrap rows in pytest.param and set id to name each case:
Now a failure reads test_slugify_named[punctuation], and you can run one case with pytest -k punctuation or the full node ID in quotes: pytest "test_slugs.py::test_slugify_named[empty-string]". You can also pass ids=[...] to the decorator, or an ids function that builds a label from each value.
When you apply several parametrize decorators to the same test, pytest runs the cross product of all values.
This produces six tests (3 times 2). It is handy for checking every pairing, for example every browser with every locale. It grows quickly, so avoid it when the cases are really a fixed list of known pairs: use one decorator with explicit rows instead.
A single row can carry a marker. This keeps a known bug visible without turning the whole suite red.
The accented case is reported as xfailed today. Once you fix the function, pytest reports it as XPASS, which tells you to remove the marker. Use marks=pytest.mark.skip(reason="...") when a case should not run at all.
Fixtures can be parametrized too. Pass params to @pytest.fixture, and every test using that fixture runs once per parameter:
For larger tables, define the cases once at module level and pass the variable in, or load them from a JSON or CSV file when the module is imported:
Keep the data free of side effects, because pytest evaluates it at collection time, before any test runs.
| Mistake | What happens | Fix |
|---|---|---|
| Argument name in the string does not match a function parameter | Error at collection: function does not use the argument | Make names identical |
| Names given as separate strings in a list, with a single value per row, but rows are tuples | Wrong number of values | Use one string "a, b" and tuples of the same length |
| Mutable values (lists, dicts) shared across rows | One test changes data seen by the next | Create fresh objects per row or in a fixture |
| Hundreds of stacked combinations | Slow suite | Use explicit rows or property-based tests |
| Computing expected values with the same logic as the code | The test can never fail | Write expected values by hand |
Yes. Put the decorator on a method, or on the class to apply it to every method. In the class case every method must accept the named arguments.
Use -k with part of the ID, or give the full node ID in quotes, such as pytest "test_slugs.py::test_slugify_named[punctuation]".
A loop is a single test that stops at the first failure. Parametrize creates independent tests, each reported, selected and rerun on its own.
Yes, any iterable works, including a list comprehension or data read from a file. Generate it at import time, and keep it deterministic so test IDs stay stable between runs.
Use @pytest.mark.parametrize("name1, name2", [...]) to run one test body over many inputs, and each row becomes its own reported test. Name rows with pytest.param(..., id=...), attach xfail or skip to single rows through marks, and stack decorators only when you truly want every combination. Write expected values by hand so the test checks behaviour rather than repeating the implementation.