Challenge - 5 Problems
Indirect Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest test using indirect fixture parameterization
What will be the output of the following pytest test run?
Consider the test and fixture below:
Consider the test and fixture below:
PyTest
import pytest @pytest.fixture def data(request): return request.param * 2 @pytest.mark.parametrize('data', [1, 3, 5], indirect=True) def test_double(data): assert data % 2 == 0 print(f"Value: {data}")
Attempts:
2 left
💡 Hint
Remember that the fixture multiplies the input by 2, making all outputs even.
✗ Incorrect
The fixture 'data' receives the parameter via 'request.param' and returns it multiplied by 2. Since all inputs (1,3,5) multiplied by 2 are even numbers, the assertion 'data % 2 == 0' passes for all. The print statements show doubled values.
❓ assertion
intermediate2:00remaining
Correct assertion for indirect fixture parameter
Given this indirect fixture parameterization, which assertion correctly verifies the fixture output?
Code snippet:
Code snippet:
import pytest
@pytest.fixture
def number(request):
return request.param + 5
@pytest.mark.parametrize('number', [0, 2, 4], indirect=True)
def test_number(number):
# Which assertion is correct here?
pass
Attempts:
2 left
💡 Hint
The fixture adds 5 to each parameter value.
✗ Incorrect
The fixture adds 5 to each parameter (0,2,4), so the expected outputs are 5,7,9. Option B correctly asserts this. Option B checks original params, C is invalid because 'request' is not defined in test, A is false because 7 and 9 are not divisible by 5.
🔧 Debug
advanced2:00remaining
Identify the error in indirect fixture usage
Why does this pytest test fail to run correctly?
Code:
Code:
import pytest
@pytest.fixture
def item(request):
return request.param * 3
@pytest.mark.parametrize('item', [1, 2, 3])
def test_item(item):
assert item % 3 == 0
Attempts:
2 left
💡 Hint
Check how parameters are passed to fixtures when using indirect parameterization.
✗ Incorrect
Without 'indirect=True', pytest passes the parameters directly to the test function, not the fixture. The fixture tries to access 'request.param' but 'request' is undefined, causing an error. Adding 'indirect=True' fixes this.
❓ framework
advanced2:00remaining
Best practice for complex indirect fixture parameterization
You want to test a function with multiple complex inputs using pytest indirect fixtures. Which approach is best?
Options:
Options:
Attempts:
2 left
💡 Hint
Think about how to handle complex data cleanly and reuse fixture logic.
✗ Incorrect
Using a single fixture that accepts a dictionary parameter allows clean handling of complex inputs. Marking parametrize with indirect=True passes the dictionary to the fixture for setup. Other options either avoid fixtures or misuse parametrize, reducing test clarity and reusability.
🧠 Conceptual
expert2:00remaining
Why use indirect parameterization in pytest fixtures?
Which statement best explains the purpose of using indirect parameterization with pytest fixtures?
Attempts:
2 left
💡 Hint
Think about how indirect=True changes parameter passing behavior.
✗ Incorrect
Indirect parameterization tells pytest to send parameters to the fixture, not the test function. This lets fixtures use parameters to prepare test data or setup. Other options describe unrelated pytest features.