Recall & Review
beginner
What is a database fixture in pytest?
A database fixture in pytest is a setup function that prepares a test database environment before tests run and cleans it up afterward. It helps tests run with consistent data and state.
Click to reveal answer
beginner
Why use database fixtures instead of creating data inside each test?
Using database fixtures avoids repeating setup code in every test. It ensures tests start with a known database state, making tests faster, cleaner, and easier to maintain.
Click to reveal answer
intermediate
What is the difference between function-scoped and session-scoped database fixtures?
Function-scoped fixtures run before and after each test function, giving a fresh database state every time. Session-scoped fixtures run once per test session, sharing the same database state across tests.
Click to reveal answer
intermediate
How can you ensure database changes made during a test do not affect other tests?
You can use transactions that roll back after each test or recreate the database state using fixtures to isolate tests and keep the database clean.
Click to reveal answer
beginner
What is a common pattern to load initial test data using pytest fixtures?
A common pattern is to create a fixture that inserts known test data into the database before tests run, so tests can rely on this data without setting it up themselves.
Click to reveal answer
What does a pytest database fixture typically do?
✗ Incorrect
Database fixtures prepare the database before tests and clean it after, ensuring consistent test environments.
Which fixture scope gives a fresh database state for each test function?
✗ Incorrect
Function-scoped fixtures run before each test function, providing a clean database state every time.
How can pytest fixtures help avoid test interference in database tests?
✗ Incorrect
Fixtures isolate tests by preparing and cleaning the database, preventing one test's changes from affecting others.
What is a benefit of using session-scoped database fixtures?
✗ Incorrect
Session-scoped fixtures run once per test session, reducing setup time by reusing the database state.
What is a common way to reset database changes after a test?
✗ Incorrect
Using transactions that roll back after tests keeps the database clean and ready for the next test.
Explain how database fixtures help maintain test isolation in pytest.
Think about how tests share or reset data.
You got /4 concepts.
Describe the difference between function-scoped and session-scoped database fixtures and when to use each.
Consider test speed versus data freshness.
You got /5 concepts.