Challenge - 5 Problems
Fixture Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest fixture dependency execution order
Given the following pytest code with fixtures depending on each other, what will be the output when running the test?
PyTest
import pytest @pytest.fixture def fixture_a(): print('Setup A') yield 'A' print('Teardown A') @pytest.fixture def fixture_b(fixture_a): print('Setup B') yield f'B uses {fixture_a}' print('Teardown B') @pytest.fixture def fixture_c(fixture_b): print('Setup C') yield f'C uses {fixture_b}' print('Teardown C') def test_example(fixture_c): print(f'Running test with {fixture_c}')
Attempts:
2 left
💡 Hint
Remember that pytest runs fixtures in dependency order: the deepest dependency first, then upwards.
✗ Incorrect
Fixture 'fixture_c' depends on 'fixture_b', which depends on 'fixture_a'. So pytest runs setup in order: fixture_a, fixture_b, fixture_c. Then the test runs. Then teardown happens in reverse order: fixture_c, fixture_b, fixture_a.
❓ assertion
intermediate1:30remaining
Correct assertion for fixture dependency value
Given these fixtures, which assertion correctly verifies that the test receives the expected combined fixture value?
PyTest
import pytest @pytest.fixture def base(): return 5 @pytest.fixture def dependent(base): return base * 2 def test_value(dependent): # Which assertion is correct here?
Attempts:
2 left
💡 Hint
The dependent fixture doubles the base fixture value.
✗ Incorrect
The base fixture returns 5. The dependent fixture returns base * 2, which is 10. So the assertion must check for 10.
🔧 Debug
advanced1:30remaining
Identify the error in fixture dependency usage
What error will occur when running this pytest code?
PyTest
import pytest @pytest.fixture def fix1(): return 1 @pytest.fixture def fix2(): return 2 def test_error(fix1, fix3): assert fix1 + fix3 == 3
Attempts:
2 left
💡 Hint
Check if all fixtures used in the test are defined.
✗ Incorrect
The test requests a fixture named 'fix3' which is not defined anywhere. Pytest raises a fixture lookup error indicating 'fix3' is missing.
❓ framework
advanced1:30remaining
Best practice for fixture dependency scope
If fixture B depends on fixture A, and fixture A has scope='module', what is the best scope to assign to fixture B to avoid scope mismatch errors?
Attempts:
2 left
💡 Hint
Fixture scope must be the same or broader than its dependencies.
✗ Incorrect
A fixture cannot have a narrower scope than a fixture it depends on. Since fixture A is 'module' scope, fixture B must be 'module' or broader (like 'session'). 'function' or 'class' are narrower and cause errors.
🧠 Conceptual
expert2:00remaining
Why use fixture dependencies in pytest?
Which of the following is the main advantage of using fixture dependencies (fixtures using other fixtures) in pytest?
Attempts:
2 left
💡 Hint
Think about how fixtures can share common setup steps.
✗ Incorrect
Fixture dependencies allow you to build complex setups by reusing simpler fixtures. This reduces code duplication and improves maintainability.