Parametrizing with indirect fixtures lets you run the same test with different setup data, making tests cleaner and easier to manage.
Parametrize with indirect fixtures in PyTest
@pytest.mark.parametrize('fixture_name', [param1, param2], indirect=True) def test_example(fixture_name): # test code using fixture_name
Use indirect=True to tell pytest to pass parameters to the fixture, not directly to the test.
The fixture must accept a request object to access the parameter.
@pytest.fixture def data(request): return request.param * 2 @pytest.mark.parametrize('data', [1, 2, 3], indirect=True) def test_double(data): assert data % 2 == 0
@pytest.fixture def setup_env(request): env = {'mode': request.param} return env @pytest.mark.parametrize('setup_env', ['dev', 'prod'], indirect=True) def test_env_mode(setup_env): assert setup_env['mode'] in ['dev', 'prod']
This test runs three times with numbers 10, 50, and 100. It prints the number and checks it is a multiple of 10.
import pytest @pytest.fixture def number(request): # Multiply parameter by 10 return request.param * 10 @pytest.mark.parametrize('number', [1, 5, 10], indirect=True) def test_is_multiple_of_ten(number): print(f'Testing number: {number}') assert number % 10 == 0
Indirect parametrization helps when test inputs need processing before use.
Always ensure your fixture uses request.param to get the parameter value.
Use descriptive parameter names to keep tests readable.
Parametrize with indirect fixtures lets you pass parameters to fixtures, not directly to tests.
This approach helps separate setup logic from test logic.
It makes tests cleaner and easier to maintain when testing multiple inputs.
Practice
indirect=True in @pytest.mark.parametrize do in pytest?Solution
Step 1: Understand parametrize behavior
Normally, parameters are passed directly to the test function as arguments.Step 2: Effect of indirect=True
Whenindirect=Trueis set, pytest sends the parameter value to the fixture instead, allowing setup logic to use it.Final Answer:
It passes the parameter value to a fixture instead of directly to the test function. -> Option DQuick Check:
indirect=True means parameter goes to fixture [OK]
- Thinking parameters always go directly to test functions
- Confusing indirect=True with skipping tests
- Assuming indirect disables fixtures
data for values 1 and 2, when there are other direct parameters?Solution
Step 1: Recall indirect parameter usage
When only some parameters are indirect, you pass a list of their names toindirect.Step 2: Apply to single parameter
For one parameterdata,indirect=['data']is correct syntax.Final Answer:
@pytest.mark.parametrize('data', [1, 2], indirect=['data']) -> Option BQuick Check:
indirect=['param_name'] for indirect fixture [OK]
- Using indirect=True for single param when multiple params exist
- Passing indirect=False or None which disables indirect
- Not using a list for indirect parameter names
import pytest
@pytest.fixture
def data(request):
return request.param * 2
@pytest.mark.parametrize('data', [3, 5], indirect=True)
def test_double(data):
print(data)Solution
Step 1: Understand fixture with request.param
The fixturedatareturnsrequest.param * 2, so it doubles the parameter.Step 2: Parametrize with indirect=True
Parameters 3 and 5 are passed to the fixture, so the test receives 6 and 10.Final Answer:
6 and 10 -> Option AQuick Check:
Fixture doubles param: 3*2=6, 5*2=10 [OK]
- Thinking parameters go directly to test, so expecting 3 and 5
- Assuming request.param is unavailable causing error
- Confusing fixture return with test parameter
@pytest.fixture
def setup_data(request):
return request.param + 1
@pytest.mark.parametrize('setup_data', [10, 20])
def test_increment(setup_data):
assert setup_data in [11, 21]Solution
Step 1: Check parametrize usage with fixture
Parameters must be passed indirectly to fixtures usingindirect=True.Step 2: Identify missing indirect=True
The code lacksindirect=True, so parameters go directly to test, not fixture.Final Answer:
Missing indirect=True in parametrize to pass parameters to fixture. -> Option CQuick Check:
indirect=True needed to use request.param [OK]
- Forgetting indirect=True causes request.param error
- Thinking fixture and param names must differ
- Assuming fixture cannot return values
config that reads a file path from parameters and loads data. You have two file paths: "file1.json" and "file2.json". How do you correctly parametrize the test to pass these paths to the fixture using indirect parametrization, when there are other direct parameters?Solution
Step 1: Identify indirect parameter usage for single fixture
When passing parameters to a fixture, useindirect=['fixture_name']to mark that parameter indirect.Step 2: Apply to config fixture
Since onlyconfigis indirect, useindirect=['config']with the list of file paths.Final Answer:
@pytest.mark.parametrize('config', ["file1.json", "file2.json"], indirect=['config']) -> Option AQuick Check:
Use indirect=['config'] to pass params to fixture [OK]
- Using indirect=True instead of list for single param
- Not using indirect causing fixture to miss parameters
- Setting indirect=False disables indirect passing
Ready to prove it?
Pass the test and this pattern moves unknown
