Challenge - 5 Problems
Dynamic Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"]
Attempts:
2 left
💡 Hint
Think about how pytest runs tests with parameterized fixtures.
✗ Incorrect
The fixture 'dynamic_data' is parameterized with two fixture names. pytest runs the test twice, once with 'data_a' and once with 'data_b'. So the print outputs 'Data A' then 'Data B'.
❓ assertion
intermediate1: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
Attempts:
2 left
💡 Hint
Check which assertion logically matches the possible values.
✗ Incorrect
Option B correctly asserts that number is one of the two possible values. Option B works but is less clear. Option B is always false. Option B is always true and does not test correctly.
🔧 Debug
advanced2: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"]
Attempts:
2 left
💡 Hint
Check the fixture name passed to getfixturevalue.
✗ Incorrect
The code tries to get a fixture named 'fix3' which does not exist, causing a FixtureLookupError.
❓ framework
advanced2: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?
Attempts:
2 left
💡 Hint
Consider pytest's built-in features for fixture parameterization.
✗ Incorrect
Option C uses pytest's recommended way to select fixtures dynamically by name. Options B and C break fixture isolation and reusability. Option C is complex and not intended for this purpose.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about pytest's rules for fixture scope hierarchy.
✗ Incorrect
pytest does not allow mixing fixtures with incompatible scopes dynamically because it cannot guarantee consistent fixture lifetimes, so it raises a scope mismatch error.