Challenge - 5 Problems
Fixture Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of pytest fixtures
Why do pytest fixtures help in writing tests?
Attempts:
2 left
💡 Hint
Think about how to avoid writing the same setup steps in every test.
✗ Incorrect
Fixtures provide a way to write setup code once and reuse it in many tests, making tests cleaner and easier to maintain.
❓ Predict Output
intermediate2:00remaining
Output of test using fixture
What will be the output when running this pytest test with a 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
The fixture returns a list whose sum is 6.
✗ Incorrect
The fixture sample_data returns [1, 2, 3]. The test asserts sum is 6, which is true, so it prints 'Test passed'.
❓ assertion
advanced2:00remaining
Correct assertion for fixture data
Given a fixture that returns a dictionary, which assertion correctly checks if the key 'status' equals 'ok'?
PyTest
import pytest @pytest.fixture def response(): return {'status': 'ok', 'code': 200} def test_response(response): # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Remember how to access dictionary values and write assertions.
✗ Incorrect
Option D correctly accesses the dictionary key and compares it. Option D is invalid syntax for dict, C uses assignment instead of comparison, D asserts the opposite.
🔧 Debug
advanced2:00remaining
Identify error in fixture usage
What will happen when running this test code?
PyTest
import pytest def test_example(sample_fixture): assert sample_fixture == 10 @pytest.fixture def sample_fixture(): return 10
Attempts:
2 left
💡 Hint
Pytest collects fixtures independently of their definition order in the source file.
✗ Incorrect
No error, the test passes. Pytest discovers all fixtures marked with @pytest.fixture in the module, regardless of whether they are defined before or after the test functions.
❓ framework
expert2:00remaining
Fixture scope impact on test runs
If a pytest fixture is defined with scope='module', how many times will it run if there are 3 test functions in the same module using it?
Attempts:
2 left
💡 Hint
Think about what 'module' scope means for fixture lifetime.
✗ Incorrect
A fixture with scope='module' runs once per module, so it runs once before all tests in that module that use it.