Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand parametrize behavior

    Normally, parameters are passed directly to the test function as arguments.
  2. 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.
  3. Final Answer:

    It passes the parameter value to a fixture instead of directly to the test function. -> Option D
  4. 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

  1. Step 1: Recall indirect parameter usage

    When only some parameters are indirect, you pass a list of their names to indirect.
  2. Step 2: Apply to single parameter

    For one parameter data, indirect=['data'] is correct syntax.
  3. Final Answer:

    @pytest.mark.parametrize('data', [1, 2], indirect=['data']) -> Option B
  4. 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?
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)
medium
A. 6 and 10
B. 3 and 5
C. None and None
D. Error: request.param not found

Solution

  1. Step 1: Understand fixture with request.param

    The fixture data returns request.param * 2, so it doubles the parameter.
  2. Step 2: Parametrize with indirect=True

    Parameters 3 and 5 are passed to the fixture, so the test receives 6 and 10.
  3. Final Answer:

    6 and 10 -> Option A
  4. Quick Check:

    Fixture doubles param: 3*2=6, 5*2=10 [OK]
Hint: Indirect param goes to fixture; fixture doubles it [OK]
Common Mistakes:
  • Thinking parameters go directly to test, so expecting 3 and 5
  • Assuming request.param is unavailable causing error
  • Confusing fixture return with test parameter
4. What is wrong with this pytest code snippet?
@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]
medium
A. The fixture should not return a value.
B. Fixture name and parameter name must be different.
C. Missing indirect=True in parametrize to pass parameters to fixture.
D. The assert statement is incorrect.

Solution

  1. Step 1: Check parametrize usage with fixture

    Parameters must be passed indirectly to fixtures using indirect=True.
  2. Step 2: Identify missing indirect=True

    The code lacks indirect=True, so parameters go directly to test, not fixture.
  3. Final Answer:

    Missing indirect=True in parametrize to pass parameters to fixture. -> Option C
  4. Quick Check:

    indirect=True needed to use request.param [OK]
Hint: Add indirect=True to parametrize when using request.param [OK]
Common Mistakes:
  • Forgetting indirect=True causes request.param error
  • Thinking fixture and param names must differ
  • Assuming fixture cannot return values
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

  1. Step 1: Identify indirect parameter usage for single fixture

    When passing parameters to a fixture, use indirect=['fixture_name'] to mark that parameter indirect.
  2. Step 2: Apply to config fixture

    Since only config is indirect, use indirect=['config'] with the list of file paths.
  3. Final Answer:

    @pytest.mark.parametrize('config', ["file1.json", "file2.json"], indirect=['config']) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard