Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of a database rollback fixture in pytest?
A database rollback fixture ensures that any changes made to the database during a test are undone after the test finishes. This keeps the database clean and consistent for other tests.
Click to reveal answer
beginner
How does a rollback fixture help maintain test isolation?
By rolling back database changes after each test, the fixture prevents one test's data from affecting another test, ensuring tests run independently and reliably.
Click to reveal answer
intermediate
Which pytest fixture scope is commonly used for database rollback fixtures and why?
The 'function' scope is commonly used because it rolls back changes after each test function, ensuring a fresh database state for every test.
Click to reveal answer
intermediate
Example snippet: How do you start a transaction and rollback after a test using pytest fixtures?
Use a fixture that begins a transaction before the test and rolls it back after. For example:
Why is it better to use rollback fixtures instead of manually cleaning the database after tests?
Rollback fixtures are faster and less error-prone because they undo changes automatically within a transaction, avoiding complex cleanup code and reducing test flakiness.
Click to reveal answer
What does a database rollback fixture do in pytest?
ADeletes the entire database
BReverts database changes after each test
CCommits database changes permanently
DCreates a new database for each test
✗ Incorrect
A rollback fixture reverts any database changes made during a test to keep the database clean.
Which pytest fixture scope is best for database rollback to isolate tests?
Amodule
Bsession
Cfunction
Dclass
✗ Incorrect
The 'function' scope runs the fixture for each test function, ensuring isolation.
Why use transactions with rollback in tests?
ATo undo changes automatically after tests
BTo keep database changes permanent
CTo speed up database queries
DTo backup the database
✗ Incorrect
Transactions allow changes to be undone automatically by rolling back after tests.
What happens if you don't rollback database changes after tests?
ATests may interfere with each other
BTests run faster
CDatabase stays clean
DDatabase size decreases
✗ Incorrect
Without rollback, leftover data can cause tests to affect each other, causing failures.
In pytest, how do you ensure a rollback happens after a test?
ARestart the database server
BCall commit() after test
CDelete database manually
DUse yield in fixture and rollback after yield
✗ Incorrect
Using yield in a fixture lets you run rollback code after the test finishes.
Explain how a database rollback fixture works in pytest and why it is important for test reliability.
Think about how transactions can undo changes automatically.
You got /5 concepts.
Describe the steps to create a pytest fixture that rolls back database changes after each test function.
Use yield in the fixture to separate setup and teardown.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a database rollback fixture in pytest?
easy
A. To permanently save test data for later use
B. To speed up database queries during tests
C. To create new database tables before tests
D. To undo database changes after each test to keep tests independent
Solution
Step 1: Understand the role of rollback fixtures
Rollback fixtures undo any changes made to the database during a test to keep tests isolated.
Step 2: Compare options with rollback purpose
Only To undo database changes after each test to keep tests independent describes undoing changes after tests, which matches rollback behavior.
Final Answer:
To undo database changes after each test to keep tests independent -> Option D
Quick Check:
Rollback fixture purpose = undo changes [OK]
Hint: Rollback means undo changes after test [OK]
Common Mistakes:
Confusing rollback with speeding up queries
Thinking rollback creates tables
Assuming rollback saves data permanently
2. Which of the following is the correct way to define a pytest fixture that rolls back database changes after a test?
easy
A. @pytest.fixture
def db_fixture():
setup_db()
yield
rollback_db()
B. @pytest.fixture
def db_fixture():
rollback_db()
yield
setup_db()
C. @pytest.fixture
def db_fixture():
yield
setup_db()
rollback_db()
D. @pytest.fixture
def db_fixture():
setup_db()
rollback_db()
yield
Solution
Step 1: Understand yield usage in fixtures
Yield separates setup (before yield) and teardown (after yield) in pytest fixtures.
Step 2: Identify correct order for rollback
Setup happens before yield, rollback (cleanup) after yield. @pytest.fixture
def db_fixture():
setup_db()
yield
rollback_db() follows this order.
Final Answer:
@pytest.fixture\ndef db_fixture():\n setup_db()\n yield\n rollback_db() -> Option A
Quick Check:
Setup before yield, rollback after yield [OK]
Hint: Setup before yield, cleanup after yield [OK]
Common Mistakes:
Placing rollback before yield
Calling setup after yield
Not using yield at all
3. Given this pytest fixture and test, what will be the final count of records in the database after the test runs?
C. Rollback is called before yield, so changes are undone before test runs
D. Rollback_db should be called twice for safety
Solution
Step 1: Check order of setup, yield, and rollback
Rollback must happen after yield to undo changes after test runs.
Step 2: Identify error in fixture code
Rollback is called before yield, so changes are undone before test, not after.
Final Answer:
Rollback is called before yield, so changes are undone before test runs -> Option C
Quick Check:
Rollback after yield for cleanup [OK]
Hint: Rollback must be after yield to undo test changes [OK]
Common Mistakes:
Calling rollback before yield
Forgetting yield entirely
Calling setup after yield
5. You want to write a pytest fixture that starts a database transaction before each test and rolls it back after, ensuring tests run fast and isolated. Which fixture code correctly achieves this behavior?
hard
A. @pytest.fixture
def db_transaction():
yield
start_transaction()
rollback_transaction()
B. @pytest.fixture
def db_transaction():
start_transaction()
yield
rollback_transaction()
C. @pytest.fixture
def db_transaction():
start_transaction()
rollback_transaction()
yield
D. @pytest.fixture
def db_transaction():
rollback_transaction()
start_transaction()
yield
Solution
Step 1: Understand transaction lifecycle in fixtures
Start transaction before yield to begin test with transaction active.
Step 2: Rollback after yield to undo changes after test
Rollback must happen after yield to clean up changes made during test.
Final Answer:
@pytest.fixture\ndef db_transaction():\n start_transaction()\n yield\n rollback_transaction() -> Option B
Quick Check:
Start before yield, rollback after yield [OK]
Hint: Start transaction before yield, rollback after yield [OK]