0
0
PyTesttesting~20 mins

Database rollback fixtures in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Rollback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the test outcome with this rollback fixture?
Given the following pytest fixture that uses a database transaction rollback, what will be the output of the test function?
PyTest
import pytest

@pytest.fixture
def db_transaction(db_connection):
    transaction = db_connection.begin()
    yield
    transaction.rollback()

def test_insert(db_transaction, db_connection):
    db_connection.execute("INSERT INTO users (id, name) VALUES (1, 'Alice')")
    result = db_connection.execute("SELECT COUNT(*) FROM users").fetchone()[0]
    print(result)
A1
B0
CRaises an exception because of missing commit
DNone
Attempts:
2 left
💡 Hint
Think about when the rollback happens in relation to the test execution.
assertion
intermediate
1:30remaining
Which assertion correctly verifies rollback behavior?
You want to test that after a test using a rollback fixture, the database table 'orders' is empty. Which assertion in the test will correctly verify this?
PyTest
def test_orders_empty_after_rollback(db_connection):
    count = db_connection.execute("SELECT COUNT(*) FROM orders").fetchone()[0]
    # Which assertion is correct here?
Aassert count is None
Bassert count != 0
Cassert count == 0
Dassert count > 0
Attempts:
2 left
💡 Hint
Rollback should remove all inserted rows after the test.
🔧 Debug
advanced
2:30remaining
Why does this rollback fixture not undo changes?
Consider this pytest fixture: @pytest.fixture def db_rollback(db_connection): yield db_connection.rollback() A test using this fixture inserts a row, but the row remains after the test. Why?
ARollback is called before yield, so changes are undone too early
BThe fixture commits changes before rollback, making rollback ineffective
CThe database connection does not support rollback
DThe transaction was never started, so rollback has no effect
Attempts:
2 left
💡 Hint
Think about what is missing before yield to start a transaction.
framework
advanced
1:30remaining
Which pytest fixture scope is best for database rollback tests?
You want to run tests that modify the database but rollback after each test to keep tests isolated. Which pytest fixture scope is best to achieve this?
Afunction
Bmodule
Csession
Dclass
Attempts:
2 left
💡 Hint
Rollback should happen after each individual test.
🧠 Conceptual
expert
3:00remaining
Why use database rollback fixtures instead of test data cleanup?
In automated testing, why is using a database rollback fixture preferred over manually deleting test data after tests?
AManual deletion is faster but less reliable than rollback
BRollback is faster and guarantees test isolation by undoing all changes atomically
CRollback requires less database permissions than manual deletion
DManual deletion can rollback changes automatically
Attempts:
2 left
💡 Hint
Think about atomicity and test isolation.