0
0
PyTesttesting~20 mins

Parametrize with indirect fixtures in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Indirect Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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:
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}")
ATest passes for all 3 inputs and prints: Value: 2, Value: 6, Value: 10
BTest fails on input 1 because 2 % 2 != 0
CTest fails on input 3 because 6 % 2 != 0
DTest fails on input 5 because 10 % 2 != 0
Attempts:
2 left
💡 Hint
Remember that the fixture multiplies the input by 2, making all outputs even.
assertion
intermediate
2:00remaining
Correct assertion for indirect fixture parameter
Given this indirect fixture parameterization, which assertion correctly verifies the fixture output?

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
Aassert number in [0, 2, 4]
Bassert number in [5, 7, 9]
Cassert number == request.param + 5
Dassert number % 5 == 0
Attempts:
2 left
💡 Hint
The fixture adds 5 to each parameter value.
🔧 Debug
advanced
2:00remaining
Identify the error in indirect fixture usage
Why does this pytest test fail to run correctly?

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
AFixture 'item' does not return a value
BAssertion is incorrect; item % 3 == 0 is always false
CMissing 'indirect=True' in parametrize causes 'request.param' to be undefined
DParameter list must be strings, not integers
Attempts:
2 left
💡 Hint
Check how parameters are passed to fixtures when using indirect parameterization.
framework
advanced
2: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:
AUse a single fixture that accepts a dictionary parameter and mark parametrize with indirect=True
BUse multiple fixtures each with separate parametrize decorators without indirect=True
CPass all parameters directly to the test function without fixtures
DUse global variables to store test data and avoid fixtures
Attempts:
2 left
💡 Hint
Think about how to handle complex data cleanly and reuse fixture logic.
🧠 Conceptual
expert
2:00remaining
Why use indirect parameterization in pytest fixtures?
Which statement best explains the purpose of using indirect parameterization with pytest fixtures?
AIt converts test functions into fixtures for reuse.
BIt automatically runs tests in parallel to speed up execution.
CIt disables fixture caching to ensure fresh data for each test.
DIt allows parameters to be passed to fixtures instead of directly to test functions, enabling setup logic based on parameters.
Attempts:
2 left
💡 Hint
Think about how indirect=True changes parameter passing behavior.