Challenge - 5 Problems
Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of composed pytest fixtures
What is the output when running this pytest test with the given fixtures?
PyTest
import pytest @pytest.fixture def base_data(): return [1, 2, 3] @pytest.fixture def extended_data(base_data): return base_data + [4, 5] def test_sum(extended_data): assert sum(extended_data) == 15 print(sum(extended_data))
Attempts:
2 left
💡 Hint
Think about how fixtures can use other fixtures as input and how the list is extended.
✗ Incorrect
The base_data fixture returns [1, 2, 3]. The extended_data fixture adds [4, 5] to it, resulting in [1, 2, 3, 4, 5]. The sum is 15, so the assertion passes and 15 is printed.
❓ assertion
intermediate2:00remaining
Correct assertion for nested fixture output
Given these fixtures, which assertion correctly verifies the final output in the test?
PyTest
import pytest @pytest.fixture def user(): return {'name': 'Alice', 'age': 30} @pytest.fixture def user_with_email(user): user['email'] = 'alice@example.com' return user def test_user_email(user_with_email): # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Check how the email is added in the second fixture and the age remains unchanged.
✗ Incorrect
The user_with_email fixture adds the email key with value 'alice@example.com' to the user dictionary. The age remains 30. So the assertion in option D correctly checks both.
🔧 Debug
advanced2:00remaining
Identify the cause of fixture reuse error
Why does this pytest test raise an error about fixture scope when running?
PyTest
import pytest @pytest.fixture(scope='function') def db_connection(): return 'db_conn' @pytest.fixture(scope='module') def user_session(db_connection): return f'session using {db_connection}' def test_session(user_session): assert 'session' in user_session
Attempts:
2 left
💡 Hint
Think about pytest fixture scopes and their allowed dependencies.
✗ Incorrect
In pytest, a fixture with a wider scope (module) cannot depend on a fixture with a narrower scope (function) because the narrower scope fixture might be destroyed before the wider scope fixture finishes. Here, 'user_session' (module scope) depends on 'db_connection' (function scope), causing a scope mismatch error.
❓ framework
advanced2:00remaining
Best practice for fixture composition in pytest
Which option correctly describes a best practice when composing fixtures in pytest?
Attempts:
2 left
💡 Hint
Think about how pytest manages fixture lifetimes and dependencies.
✗ Incorrect
In pytest, a fixture with a wider scope cannot depend on a fixture with a narrower scope because the narrower scope fixture might be destroyed before the wider scope fixture finishes. So keeping scopes compatible is a best practice.
🧠 Conceptual
expert3:00remaining
Understanding fixture teardown order in composed fixtures
Given these fixtures, in what order will the teardown code run after the test completes?
PyTest
import pytest @pytest.fixture def resource_a(): print('Setup A') yield 'A' print('Teardown A') @pytest.fixture def resource_b(resource_a): print('Setup B') yield 'B' print('Teardown B') def test_resources(resource_b): print('Test running')
Attempts:
2 left
💡 Hint
Fixtures teardown in reverse order of setup, respecting dependencies.
✗ Incorrect
resource_a is setup first, then resource_b. The test runs. Then teardown happens in reverse: resource_b teardown first, then resource_a.