Challenge - 5 Problems
Database Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest fixture scope behavior
Consider the following pytest fixtures and test functions. What will be the output when running the tests?
PyTest
import pytest @pytest.fixture(scope="module") def db_connection(): print("Setup DB connection") yield "connection" print("Teardown DB connection") @pytest.fixture(scope="function") def db_cursor(db_connection): print("Setup DB cursor") yield "cursor" print("Teardown DB cursor") def test_query1(db_cursor): print("Running test_query1") assert db_cursor == "cursor" def test_query2(db_cursor): print("Running test_query2") assert db_cursor == "cursor"
Attempts:
2 left
💡 Hint
Remember that module-scoped fixtures run once per module, function-scoped fixtures run per test function.
✗ Incorrect
The module-scoped fixture 'db_connection' sets up once before any tests run and tears down after all tests. The function-scoped fixture 'db_cursor' sets up and tears down around each test function. So the output shows setup and teardown of cursor twice, but connection only once.
❓ assertion
intermediate1:30remaining
Correct assertion for database fixture test
You have a pytest fixture that returns a database session object. Which assertion correctly verifies that the session is active during a test?
PyTest
import pytest @pytest.fixture def db_session(): class Session: def __init__(self): self.active = True def close(self): self.active = False session = Session() yield session session.close() def test_session_active(db_session): # Which assertion below is correct here? pass
Attempts:
2 left
💡 Hint
The session should be active during the test, so active should be True.
✗ Incorrect
The fixture yields a session object with active=True. The test runs while active is True. After the test, close() sets active to False. So the correct assertion during the test is that active is True.
🔧 Debug
advanced2:30remaining
Identify the cause of fixture reuse failure
Given the following pytest fixture and tests, why does the second test fail?
PyTest
import pytest @pytest.fixture(scope="function") def db_conn(): conn = open_db_connection() yield conn conn.close() def test_insert(db_conn): db_conn.insert("data") assert db_conn.count() == 1 def test_query(db_conn): result = db_conn.query("select *") assert len(result) == 1
Attempts:
2 left
💡 Hint
Think about how fixture scope affects data persistence between tests.
✗ Incorrect
Because the fixture scope is 'function', each test gets a fresh database connection. The data inserted in test_insert is not present in test_query's connection, causing the query to return empty results.
❓ framework
advanced2:00remaining
Best fixture pattern for transactional tests
Which pytest fixture pattern ensures that each test runs inside a database transaction that is rolled back after the test, keeping the database clean?
Attempts:
2 left
💡 Hint
Think about how to isolate test data changes per test.
✗ Incorrect
Starting a transaction before the test and rolling it back after ensures no test data persists. Function scope ensures this happens for each test individually.
🧠 Conceptual
expert3:00remaining
Choosing fixture scope for integration tests with database
You have a large test suite with many integration tests that use a database. Tests modify data and depend on previous data state. Which fixture scope and pattern is best to balance test speed and data consistency?
Attempts:
2 left
💡 Hint
Consider the trade-off between setup overhead and data isolation.
✗ Incorrect
Session-scoped fixtures reduce setup overhead by reusing the connection. Manual data reset between tests balances speed and consistency for large suites where full isolation per test is costly.