Pytest decorator for parametrizing tests with default iterables, providing alternative syntax for pytest.mark.parametrize.
Decorate tests with iterable default values. Other fixtures can still be used as normal.
fromparametrizedimportparametrized@parametrizeddeftest(..., name=values):
"""test single parametrized arg with each value"""@parametrized.zipdeftest(name=values, name1=values1, ...):
"""test parametrized args with zipped values"""@parametrized.productdeftest(name=values, name1=values1, ...):
"""test parametrized args with cartesian product of values"""Zip before and after example:
@pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), ("6*9", 42), ],)deftest_eval(test_input, expected):
asserteval(test_input) ==expected@parametrized.zipdeftest_eval(test_input=["3+5", "2+4", "6*9"], expected=[8, 6, 42]):
asserteval(test_input) ==expectedProduct before and after example:
@pytest.mark.parametrize("x", [0, 1])@pytest.mark.parametrize("y", [2, 3])deftest_foo(x, y):
pass@parametrized.productdeftest_foo(x=[0, 1], y=[2, 3]):
passpytest.param is supported for single values or .product.
Parametrized fixtures which simply return their param.
fixture_name=parametrized.fixture(*params, **kwargs)Before and after example:
@pytest.fixture(params=[0, 1], ids=["spam", "ham"])defa(request):
returnrequest.parama=parametrized.fixture(0, 1, ids=["spam", "ham"])pip install pytest-parametrized100% branch coverage.
pytest [--cov]