python-pytest-cases
python-pytest-cases copied to clipboard
Support indirect parametrization
Sometimes a fixture is parametrized with cases using @parametrize_with_cases, but for a specific test one would like to only select a subset of these cases. This would for example solve the need in #188
Indirect parametrization is the answer of pytest to this need. For example :
from pytest_cases import parametrize, fixture
@fixture
@parametrize(a=[0, 1])
def my_fix(a):
return a * 2
@fixture
@parametrize(b=[0, 10])
def my_fix2(b, my_fix):
return b + my_fix
def test_foo(my_fix2):
assert my_fix2 in (0, 2, 10, 12)
@parametrize(my_fix=[2], indirect=True)
def test_foo_indirect(my_fix2):
assert my_fix2 in (4, 14)
Leads to
test_indirect.py::test_foo[b=0-a=0]
test_indirect.py::test_foo[b=0-a=1]
test_indirect.py::test_foo[b=10-a=0]
test_indirect.py::test_foo[b=10-a=1]
test_indirect.py::test_foo_indirect[b=0-my_fix=2]
Apart from the test id that is a bit fiddled with by pytest, the rest works as expected. See also this article.
Now does this work with @parametrize_with_cases ? Does it work with @parametrize when there is a fixture_ref ? This needs to be investigated, and possibly, fixed
A first difficulty, even without adressing fixture_refs and cases, is to include parameter names in this indirect mechanism. Indeed, in pytest fixtures do not support several parameters so using indirect just requires the user to pass the fixture name.
With pytest-case since @fixture is compliant with multiple parametrize (multiple args), then indirect will require users to tell which arg is indirectly parametrized. In other words to pass the fixture name AND the argname(s)