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 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?
AGenerate user interface elements
BCompile the application code
CSet up and tear down database state for tests
DSend emails during tests
✗ 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?
AFunction scope
BSession scope
CModule scope
DClass scope
✗ 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?
ABy sharing the same database state for all tests
BBy isolating tests with setup and teardown steps
CBy disabling the database during tests
DBy running tests in parallel without setup
✗ 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?
AThey speed up tests by reusing setup
BThey run before every test function
CThey create a new database for each test
DThey disable database access
✗ 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?
ARestart the computer
BIgnore database changes
CDelete the test code
DUse transactions with rollback
✗ 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.
Practice
(1/5)
1. What is the main purpose of using database fixtures in pytest?
easy
A. To speed up the database server
B. To write SQL queries inside test functions
C. To prepare and clean test data automatically before and after tests
D. To replace the need for assertions in tests
Solution
Step 1: Understand what fixtures do
Fixtures in pytest are used to set up and tear down resources needed for tests, such as database data.
Step 2: Identify the role of database fixtures
Database fixtures specifically prepare test data before tests run and clean it up after tests finish, ensuring tests run reliably.
Final Answer:
To prepare and clean test data automatically before and after tests -> Option C
Quick Check:
Database fixtures = setup and cleanup [OK]
Hint: Fixtures handle setup and cleanup automatically [OK]
Common Mistakes:
Thinking fixtures run SQL queries inside tests
Believing fixtures speed up the database server
Confusing fixtures with assertions
2. Which of the following is the correct way to write a pytest fixture that sets up a database connection and tears it down after the test using yield?
easy
A. def db():
conn = connect()
yield conn
conn.close()
B. def db():
conn = connect()
conn.close()
yield conn
C. def db():
yield connect()
conn.close()
D. def db():
conn = connect()
return conn
conn.close()
Solution
Step 1: Understand yield usage in fixtures
Using yield in a fixture splits setup (before yield) and teardown (after yield).
Step 2: Check each option's order
def db():
conn = connect()
yield conn
conn.close() sets up connection, yields it, then closes connection after test. Others close before yield or have unreachable code.
A. The cleanup code after return is never executed
B. The fixture should use yield instead of return for cleanup
C. The table creation SQL is incorrect
D. The fixture is missing the @pytest.mark decorator
Solution
Step 1: Check the fixture structure
Code after return statement is unreachable and will never run.
Step 2: Understand cleanup execution
Cleanup code must run after test, so it should be placed after yield or before return, but not after return.
Final Answer:
The cleanup code after return is never executed -> Option A
Quick Check:
Code after return is unreachable [OK]
Hint: Code after return in fixture won't run [OK]
Common Mistakes:
Thinking return allows cleanup after it
Confusing yield and return usage
Ignoring unreachable code warnings
5. You want to create a pytest fixture that sets up a test database with multiple tables and ensures all tables are dropped after tests, even if a test fails. Which pattern best achieves this?
hard
A. Create tables once globally without cleanup to speed up tests
B. Create tables inside each test and drop them at the end of each test
C. Use return in fixture to return connection, then drop tables in a separate teardown function
D. Use a fixture with yield: create tables before yield, drop tables after yield
Solution
Step 1: Understand reliable setup and teardown
Using yield in fixtures allows setup before tests and guaranteed cleanup after, even if tests fail.
Step 2: Evaluate options for cleanup guarantee
Use a fixture with yield: create tables before yield, drop tables after yield uses yield to create tables before tests and drop them after, ensuring cleanup always runs.
Final Answer:
Use a fixture with yield: create tables before yield, drop tables after yield -> Option D
Quick Check:
Yield fixture ensures setup and guaranteed teardown [OK]
Hint: Yield fixtures guarantee cleanup after tests [OK]