0
0
PyTesttesting~5 mins

Database fixture patterns in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
Which fixture scope gives a fresh database state for each test function?
AFunction scope
BSession scope
CModule scope
DClass scope
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
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
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
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.