What does 'indirect=True' do in pytest's parametrize decorator?
It tells pytest to pass the parameter value to a fixture instead of directly to the test function. The fixture then processes the parameter before the test runs.
Click to reveal answer
intermediate
How do indirect fixtures help when you want to prepare test data differently for each parameter?
Indirect fixtures let you customize setup logic for each parameter value, like creating different test objects or configurations, by receiving the parameter and returning the prepared data.
Click to reveal answer
beginner
Example: What will happen if you use @pytest.mark.parametrize('input', [1, 2], indirect=True) with a fixture named 'input'?
The fixture 'input' will be called twice, once with 1 and once with 2 as parameter values. The test function will receive the fixture's return value each time.
Click to reveal answer
intermediate
Why might you prefer indirect parametrization over direct parametrization?
Because indirect parametrization allows complex setup or transformation of parameters before the test runs, making tests cleaner and more flexible.
Click to reveal answer
beginner
What is a common mistake when using indirect fixtures with parametrize?
A common mistake is forgetting to name the fixture the same as the parameter or not setting indirect=True, which causes pytest to pass the parameter directly instead of through the fixture.
Click to reveal answer
What does setting indirect=True in @pytest.mark.parametrize do?
ASkips the test for that parameter
BPasses the parameter to a fixture for processing
CRuns the test multiple times without parameters
DDirectly passes the parameter to the test function
✗ Incorrect
Setting indirect=True tells pytest to send the parameter to a fixture instead of directly to the test function.
If you parametrize 'data' with indirect=True, what must exist?
AA fixture named 'data'
BA test function named 'data'
CA global variable named 'data'
DA class named 'data'
✗ Incorrect
The parameter name must match a fixture name to use indirect parametrization.
Why use indirect parametrization in pytest?
ATo customize setup logic for each parameter
BTo skip tests automatically
CTo run tests in parallel
DTo reduce test execution time
✗ Incorrect
Indirect parametrization allows you to customize how each parameter is prepared before the test runs.
What happens if you forget indirect=True but want to use a fixture?
AThe fixture is ignored
BThe test fails to run
CThe test runs only once
DThe parameter is passed directly to the test function
✗ Incorrect
Without indirect=True, pytest passes parameters directly to the test function, not through the fixture.
Which of these is a valid use of indirect parametrization?
Option C uses indirect=True to pass 'dev' and 'prod' to a fixture named 'config' for setup.
Explain how indirect parametrization works in pytest and why it is useful.
Think about how fixtures can prepare data differently for each parameter.
You got /5 concepts.
Describe a scenario where indirect parametrization would be better than direct parametrization.
Consider when test inputs require special preparation before testing.
You got /4 concepts.
Practice
(1/5)
1. What does using indirect=True in @pytest.mark.parametrize do in pytest?
easy
A. It skips the test when the parameter is None.
B. It disables the fixture for that test.
C. It runs the test only once regardless of parameters.
D. It passes the parameter value to a fixture instead of directly to the test function.
Solution
Step 1: Understand parametrize behavior
Normally, parameters are passed directly to the test function as arguments.
Step 2: Effect of indirect=True
When indirect=True is 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 D
Quick Check:
indirect=True means parameter goes to fixture [OK]
Hint: Indirect means parameter goes to fixture, not test directly [OK]
Common Mistakes:
Thinking parameters always go directly to test functions
Confusing indirect=True with skipping tests
Assuming indirect disables fixtures
2. Which of the following is the correct syntax to parametrize a test with an indirect fixture named data for values 1 and 2, when there are other direct parameters?
easy
A. @pytest.mark.parametrize('data', [1, 2], indirect=True)
B. @pytest.mark.parametrize('data', [1, 2], indirect=['data'])
C. @pytest.mark.parametrize('data', [1, 2], indirect=False)
D. @pytest.mark.parametrize('data', [1, 2], indirect=None)
Solution
Step 1: Recall indirect parameter usage
When only some parameters are indirect, you pass a list of their names to indirect.
Step 2: Apply to single parameter
For one parameter data, indirect=['data'] is correct syntax.
Final Answer:
@pytest.mark.parametrize('data', [1, 2], indirect=['data']) -> Option B
Quick Check:
indirect=['param_name'] for indirect fixture [OK]
Hint: Use indirect=['param_name'] to mark specific params indirect [OK]
Common Mistakes:
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
3. Given the code below, what will be printed when running the test?
5. You want to test a fixture 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?
hard
A. @pytest.mark.parametrize('config', ["file1.json", "file2.json"], indirect=['config'])
B. @pytest.mark.parametrize('config', ["file1.json", "file2.json"])
C. @pytest.mark.parametrize('config', ["file1.json", "file2.json"], indirect=True)
D. @pytest.mark.parametrize('config', ["file1.json", "file2.json"], indirect=False)
Solution
Step 1: Identify indirect parameter usage for single fixture
When passing parameters to a fixture, use indirect=['fixture_name'] to mark that parameter indirect.
Step 2: Apply to config fixture
Since only config is indirect, use indirect=['config'] with the list of file paths.
Final Answer:
@pytest.mark.parametrize('config', ["file1.json", "file2.json"], indirect=['config']) -> Option A
Quick Check:
Use indirect=['config'] to pass params to fixture [OK]
Hint: Use indirect=['fixture_name'] to pass params to fixture [OK]
Common Mistakes:
Using indirect=True instead of list for single param
Not using indirect causing fixture to miss parameters
Setting indirect=False disables indirect passing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.