Challenge - 5 Problems
Fixture Scope Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest fixture with function scope
What will be the output count of the fixture usage in the following pytest code?
PyTest
import pytest count = 0 @pytest.fixture(scope='function') def my_fixture(): global count count += 1 return count def test_one(my_fixture): assert my_fixture == 1 def test_two(my_fixture): assert my_fixture == 2
Attempts:
2 left
💡 Hint
Function scope means fixture runs fresh for each test function.
✗ Incorrect
With function scope, the fixture runs separately for each test function, so count increments each time. The first test gets count=1, the second gets count=2.
❓ Predict Output
intermediate2:00remaining
Effect of class scope on fixture invocation count
Given this pytest code, what is the value of count after running all tests?
PyTest
import pytest count = 0 @pytest.fixture(scope='class') def my_fixture(): global count count += 1 return count class TestClass: def test_one(self, my_fixture): assert my_fixture == 1 def test_two(self, my_fixture): assert my_fixture == 1
Attempts:
2 left
💡 Hint
Class scope means fixture runs once per test class.
✗ Incorrect
With class scope, the fixture runs once for the entire TestClass, so count increments only once. Both tests get the same fixture value 1.
❓ assertion
advanced2:00remaining
Correct assertion for module scoped fixture reuse
Which assertion correctly verifies that a module scoped fixture is reused across multiple test functions?
PyTest
import pytest @pytest.fixture(scope='module') def resource(): return [] def test_append_one(resource): resource.append(1) # What assertion here? def test_append_two(resource): resource.append(2) # What assertion here?
Attempts:
2 left
💡 Hint
Module scope means the same fixture instance is shared across tests in the module.
✗ Incorrect
The module scoped fixture returns the same list instance for both tests. After first test, list is [1]. After second test, list is [1, 2]. So assertions must reflect that.
🔧 Debug
advanced2:00remaining
Debugging session scoped fixture with side effects
Why does this session scoped fixture cause tests to fail intermittently?
PyTest
import pytest @pytest.fixture(scope='session') def db_connection(): conn = open_db_connection() yield conn conn.close() def test_query_one(db_connection): assert db_connection.query('SELECT 1') == 1 def test_query_two(db_connection): assert db_connection.query('SELECT 2') == 2
Attempts:
2 left
💡 Hint
Session scope means fixture runs once per test session, but teardown happens after all tests.
✗ Incorrect
The fixture yields the connection once per session, but if the connection is closed too early (e.g., after first test), subsequent tests fail. Proper use of yield ensures teardown after all tests.
🧠 Conceptual
expert3:00remaining
Choosing fixture scope for expensive setup with multiple test classes
You have an expensive setup step needed for multiple test classes in a module. You want to run it only once per module but ensure each test class gets a fresh state derived from that setup. Which fixture scope strategy is best?
Attempts:
2 left
💡 Hint
Think about separating expensive setup from per-class state initialization.
✗ Incorrect
A module scoped fixture runs once for the expensive setup. A class scoped fixture can depend on it and create fresh state for each test class, balancing efficiency and isolation.