How do I use pytest fixture dependencies and params at the same time?

59 Views Asked by At

I'm trying to create a fixture that has dependencies on other fixtures and is also parameterized. Example:

@pytest.fixture(scope="module")
def fix1():
  ...

@pytest.fixture(scope="module", params=[False, True])
def fix2(fix1, param):
  ...

which gives me the error: fixture 'param' not found.

I've tried using a default arg

@pytest.fixture(scope="module", params=[False, True])
def fix2(fix1, param=True):
  ...

but this just ends up with the fixture always receiving True no matter the parameter. @pytest.mark.parameterize(..., indirect=True) on the test also just ends up with the same result as the above.

1

There are 1 best solutions below

0
tmt On

You need to use request as the fixture function's argument, and then its property param to get the value:

@pytest.fixture(scope="module", params=[False, True])
def fix2(fix1, request):
    print(request.param)
    ...