0
0
PyTesttesting~20 mins

Dynamic fixture selection in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dynamic fixture selection with parameter
What is the output of the following pytest test when run with pytest -s?
PyTest
import pytest

@pytest.fixture
def data_a():
    return "Data A"

@pytest.fixture
def data_b():
    return "Data B"

@pytest.fixture(params=["data_a", "data_b"])
def dynamic_data(request):
    return request.getfixturevalue(request.param)

def test_dynamic(dynamic_data):
    print(dynamic_data)
    assert dynamic_data in ["Data A", "Data B"]
AData B
BData B\nData A
CData A
DData A\nData B
Attempts:
2 left
💡 Hint
Think about how pytest runs tests with parameterized fixtures.
assertion
intermediate
1:30remaining
Correct assertion for dynamic fixture value
Given a dynamic fixture that returns either 10 or 20, which assertion correctly verifies the value inside the test?
PyTest
import pytest

@pytest.fixture(params=[10, 20])
def number(request):
    return request.param

def test_number(number):
    # Which assertion is correct here?
    pass
Aassert number == 10 and number == 20
Bassert number in [10, 20]
Cassert number == 10 or number == 20
Dassert number != 10 or number != 20
Attempts:
2 left
💡 Hint
Check which assertion logically matches the possible values.
🔧 Debug
advanced
2:00remaining
Identify the error in dynamic fixture usage
What error will this pytest code raise when running the test?
PyTest
import pytest

@pytest.fixture(params=["fix1", "fix2"])
def dynamic(request):
    return request.getfixturevalue("fix3")

@pytest.fixture
def fix1():
    return "Fix 1"

@pytest.fixture
def fix2():
    return "Fix 2"

def test_dynamic(dynamic):
    assert dynamic in ["Fix 1", "Fix 2"]
AFixtureLookupError: The fixture 'fix3' is not found
BTypeError: getfixturevalue() missing required positional argument
CAssertionError: dynamic value not in expected list
DNo error, test passes
Attempts:
2 left
💡 Hint
Check the fixture name passed to getfixturevalue.
framework
advanced
2:30remaining
Best practice for dynamic fixture selection in pytest
Which approach is best to dynamically select a fixture based on a test parameter in pytest?
AUse pytest hooks to modify fixture behavior at runtime
BUse if-else inside the test to call fixture functions directly
CUse a parameterized fixture with request.getfixturevalue to call fixtures by name
DUse global variables to store fixture data and select dynamically
Attempts:
2 left
💡 Hint
Consider pytest's built-in features for fixture parameterization.
🧠 Conceptual
expert
3:00remaining
Understanding fixture scope impact on dynamic fixture selection
If you have a session-scoped dynamic fixture that selects between two fixtures with different scopes (one 'function' and one 'session'), what is the expected behavior when running tests?
Apytest raises an error because fixtures with different scopes cannot be mixed dynamically
BThe dynamic fixture adopts the broader scope of the selected fixtures automatically
CThe dynamic fixture runs with the narrower scope of the selected fixture for each test run
DThe test results are unpredictable and may cause flaky tests
Attempts:
2 left
💡 Hint
Think about pytest's rules for fixture scope hierarchy.