pytest-parametrization
pytest-parametrization is a Pytest plugin that aims to provide a simpler and more explicit way to parametrize test functions compared to the built-in `@pytest.mark.parametrize` decorator. It allows for clearer definition of test cases by separating parameter names from their values. The current version is 2022.2.1, with an irregular release cadence; the last PyPI update was in May 2022.
Common errors
-
NameError: name 'parametrize' is not defined
cause The `parametrize` decorator from the plugin was used without the correct import statement.fixAdd `from parametrization import parametrize` at the top of your test file. -
TypeError: 'list' object is not callable
cause This error can occur if you mistakenly try to call a list of parameters as if it were a function, or misconfigure the `parametrize` decorator.fixDouble-check the syntax of your `@parametrize` decorator. Ensure parameter values are correctly passed as lists for each argument name, e.g., `@parametrize(arg_name=[value1, value2])`. -
AssertionError: assert X == Y (where X and Y are values)
cause A test assertion failed for one or more parametrized test cases.fixRun `pytest -v` or `pytest --pdb` to get verbose output or drop into a debugger upon failure. The detailed test IDs from `pytest-parametrization` should help identify which specific parameter set caused the failure.
Warnings
- gotcha This library provides an alternative syntax (`from parametrization import parametrize`) to the standard Pytest parametrization (`@pytest.mark.parametrize`). Users should be aware they are using a plugin with its own conventions, not the built-in decorator directly.
- gotcha When passing mutable objects (like lists or dictionaries) as parameters, `pytest` (and by extension this plugin) passes them by reference, not by copy. Modifying a mutable parameter in one test case will affect subsequent test cases that receive the same parameter instance.
- gotcha There's a known `pytest` core issue with `@pytest.mark.parametrize` causing reference leaks on newer Python versions (3.12+), preventing reliable object cleanup by the GC during interpreter shutdown. While this is a `pytest` core problem, it impacts any test using parametrization, including those with `pytest-parametrization`.
Install
-
pip install pytest-parametrization
Imports
- parametrize
from pytest import mark.parametrize
from parametrization import parametrize
Quickstart
from parametrization import parametrize
import pytest
@parametrize(
a=[1, 2],
b=[2, 4]
)
def test_addition(a, b):
assert a + b == a + b
@parametrize(
num=[(1, 'one'), (2, 'two')],
ids=['test_one', 'test_two']
)
def test_number_to_word(num, ids):
number, word = num
if number == 1:
assert word == 'one'
elif number == 2:
assert word == 'two'
# To run: pytest -v your_test_file.py