I just want to understand what does it mean or what happens if I set indirect parameter to True
or False
in the pytest.mark.parametrize
?
Asked
Active
Viewed 2.2k times
46
-
https://stackoverflow.com/questions/18011902/pass-a-parameter-to-a-fixture-function/33879151#33879151 It is used to pass a parameter to a fixture – CrazyVideoGamer Jan 04 '21 at 03:16
1 Answers
47
With indirect=True
you can parametrize your fixture, False
- default value. Example:
import pytest
@pytest.fixture
def fixture_name(request):
return request.param
@pytest.mark.parametrize('fixture_name', ['foo', 'bar'], indirect=True)
def test_indirect(fixture_name):
assert fixture_name == 'baz'
So this example generates two tests. First one gets from fixture_name
value foo, because this fixture for this test runs with parametization. Second test gets bar value. And each tests will fail, because of assert checking for baz.

Sergei Voronezhskii
- 2,184
- 19
- 32
-
2indirect=True ensures you still execute the fixture body when you use parametrize on a test. – MechanTOurS Jan 02 '18 at 12:22
-
17So `indirect` is a way to parametrize fixtures. And the main difference compared to parametrize the fixture using `pytest.fixture(params=[...])` is that the fixture can be parametrized only for the specific test function (otherwise it always parametrize for all test functions that use the fixture) ? – SuperGeo Nov 20 '18 at 00:05
-
1
-
13I just discovered that it's necessary that the fixture's argument (that will accept the parameter) is called `request`. Other names will not work, and pytest will complain that that other-named fixture does not exist. pytest 6.2.2 – t-mart Feb 11 '21 at 07:19
-