How to Combine Parametrize Decorators in pytest for Multiple Test Cases
In
pytest, you can combine multiple @pytest.mark.parametrize decorators by stacking them on a test function. This runs the test for every combination of parameters from each decorator, creating a Cartesian product of test cases.Syntax
Use multiple @pytest.mark.parametrize decorators stacked above a test function. Each decorator defines parameters and their values. pytest runs the test for all combinations of these parameters.
Example parts:
@pytest.mark.parametrize('param1', [values]): defines first parameter and its values@pytest.mark.parametrize('param2', [values]): defines second parameter and its values- Test function receives all parameters as arguments
python
import pytest @pytest.mark.parametrize('param2', ['a', 'b']) @pytest.mark.parametrize('param1', [1, 2]) def test_example(param1, param2): assert param1 in [1, 2] assert param2 in ['a', 'b']
Example
This example shows how pytest runs tests for every combination of two parameters using stacked @pytest.mark.parametrize decorators.
python
import pytest @pytest.mark.parametrize('char', ['x', 'y']) @pytest.mark.parametrize('num', [10, 20]) def test_combinations(num, char): print(f'Testing with num={num} and char={char}') assert isinstance(num, int) assert char in ['x', 'y']
Output
Testing with num=10 and char=x
Testing with num=20 and char=x
Testing with num=10 and char=y
Testing with num=20 and char=y
==== 4 passed in 0.01s ====
Common Pitfalls
Common mistakes when combining @pytest.mark.parametrize decorators include:
- Mixing parameter names incorrectly or missing parameters in the test function signature.
- Expecting parameters to combine in pairs instead of all combinations (pytest creates a Cartesian product).
- Using a single
@pytest.mark.parametrizewith multiple parameters when you want all combinations.
Correct usage stacks decorators; incorrect usage tries to combine parameters in one decorator without Cartesian product.
python
import pytest # Wrong: single parametrize with tuples (does not create all combinations) @pytest.mark.parametrize('num, char', [(10, 'x'), (20, 'y')]) def test_wrong(num, char): assert isinstance(num, int) assert char in ['x', 'y'] # Right: stacked parametrize for all combinations @pytest.mark.parametrize('char', ['x', 'y']) @pytest.mark.parametrize('num', [10, 20]) def test_right(num, char): assert isinstance(num, int) assert char in ['x', 'y']
Quick Reference
Tips for combining @pytest.mark.parametrize decorators:
- Stack decorators to get all combinations (Cartesian product).
- Order of decorators affects parameter order in test function arguments.
- Use descriptive parameter names for clarity.
- Ensure test function signature matches all parameter names.
| Tip | Description |
|---|---|
| Stack decorators | Use multiple @pytest.mark.parametrize decorators to combine parameters. |
| Parameter order | The last decorator's parameters appear first in the test function arguments. |
| Match names | Test function arguments must match all parameter names exactly. |
| Use clear names | Choose descriptive names for easier test understanding. |
Key Takeaways
Stack multiple @pytest.mark.parametrize decorators to run tests with all parameter combinations.
pytest creates a Cartesian product of parameters from stacked decorators, not just pairs.
The order of decorators affects the order of parameters in the test function signature.
Always match test function arguments to all parameter names from decorators.
Use clear parameter names and separate decorators for better test readability.