0
0
PyTesttesting~20 mins

Database fixture patterns in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"
A
Setup DB cursor
Setup DB connection
Running test_query1
Teardown DB cursor
Setup DB cursor
Running test_query2
Teardown DB cursor
Teardown DB connection
B
Setup DB connection
Setup DB cursor
Running test_query1
Setup DB cursor
Running test_query2
Teardown DB cursor
Teardown DB cursor
Teardown DB connection
C
Setup DB connection
Setup DB cursor
Running test_query1
Teardown DB cursor
Setup DB cursor
Running test_query2
Teardown DB cursor
Teardown DB connection
D
Setup DB connection
Setup DB cursor
Running test_query1
Teardown DB cursor
Teardown DB connection
Setup DB cursor
Running test_query2
Teardown DB cursor
Attempts:
2 left
💡 Hint
Remember that module-scoped fixtures run once per module, function-scoped fixtures run per test function.
assertion
intermediate
1: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
Aassert db_session.active == False
Bassert db_session.active is True
Cassert db_session is None
Dassert db_session.closed is True
Attempts:
2 left
💡 Hint
The session should be active during the test, so active should be True.
🔧 Debug
advanced
2: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
AThe connection is closed before test_insert runs, causing insert to fail.
BThe fixture does not yield the connection properly, causing the connection to be None in test_query.
CThe tests run in parallel causing race conditions on the database connection.
DThe fixture scope is 'function', so each test gets a new connection; data inserted in test_insert is lost before test_query runs.
Attempts:
2 left
💡 Hint
Think about how fixture scope affects data persistence between tests.
framework
advanced
2: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?
AUse a function-scoped fixture that starts a transaction before yield and rolls back after yield.
BUse a module-scoped fixture that commits transactions after each test.
CUse a function-scoped fixture that commits transactions before yield and rolls back after yield.
DUse a session-scoped fixture that never rolls back transactions.
Attempts:
2 left
💡 Hint
Think about how to isolate test data changes per test.
🧠 Conceptual
expert
3: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?
AUse a session-scoped fixture to create a single database connection and reset data manually between tests.
BUse function-scoped fixtures with transactions rolled back after each test to isolate data changes.
CUse module-scoped fixtures that create and tear down the database for each module of tests.
DUse class-scoped fixtures that share a connection and commit changes after each test.
Attempts:
2 left
💡 Hint
Consider the trade-off between setup overhead and data isolation.