Challenge - 5 Problems
Pytest Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a test using a simple fixture
What is the output when running this pytest test with the given fixture?
PyTest
import pytest @pytest.fixture def sample_data(): return [1, 2, 3] def test_sum(sample_data): assert sum(sample_data) == 6 print("Test passed")
Attempts:
2 left
💡 Hint
Look at what the fixture returns and what the test asserts.
✗ Incorrect
The fixture returns a list [1, 2, 3]. The test asserts that the sum is 6, which is true, so the test passes and prints 'Test passed'.
❓ assertion
intermediate1:30remaining
Correct assertion with fixture data
Given this fixture, which assertion correctly verifies the fixture data?
PyTest
import pytest @pytest.fixture def user_info(): return {"name": "Alice", "age": 30}
Attempts:
2 left
💡 Hint
Remember how to access dictionary values in Python.
✗ Incorrect
user_info is a dictionary. Accessing with user_info["name"] is correct. Option D fails because dicts don't have attribute access. Option D asserts wrong age. Option D compares int to string.
🔧 Debug
advanced2:00remaining
Identify the error in fixture usage
What error occurs when running this test code?
PyTest
import pytest @pytest.fixture def data(): return [1, 2, 3] def test_data(): assert sum(data) == 6
Attempts:
2 left
💡 Hint
Check how the fixture is used inside the test function.
✗ Incorrect
The test function does not accept the fixture as a parameter, so 'data' refers to the fixture function itself, not its returned value. sum(data) tries to sum a function, causing TypeError.
🧠 Conceptual
advanced1:00remaining
Scope of pytest fixtures
Which fixture scope causes the fixture to be created once per test session?
Attempts:
2 left
💡 Hint
Think about how long the fixture lives during test runs.
✗ Incorrect
The 'session' scope creates the fixture once for the entire test session. 'function' is per test function, 'class' per test class, 'module' per test module.
❓ framework
expert1:30remaining
Using autouse fixtures in pytest
What is the effect of setting autouse=True in a pytest fixture?
Attempts:
2 left
💡 Hint
Consider how autouse changes fixture invocation.
✗ Incorrect
Setting autouse=True makes the fixture run automatically for all tests in its scope, even if tests do not explicitly request it.