What is the purpose of the @pytest.fixture decorator in pytest?
It marks a function as a fixture, which can provide setup data or state for tests. Fixtures help reuse code and manage test dependencies.
Click to reveal answer
beginner
How do you use a fixture in a pytest test function?
You add the fixture function name as a parameter to the test function. pytest automatically runs the fixture and passes its return value.
Click to reveal answer
beginner
Can a fixture return a value? If yes, how is it used in the test?
Yes, a fixture can return a value. The test function receives this value as an argument and can use it directly.
Click to reveal answer
intermediate
What is the scope of a pytest fixture and what are some common scopes?
Scope controls how often the fixture runs. Common scopes are 'function' (default, runs per test), 'module' (runs once per module), 'class' (runs once per class), and 'session' (runs once per test session).
Click to reveal answer
intermediate
How can you clean up resources after a test using a fixture?
Use the yield statement in the fixture. Code before yield runs before the test, and code after yield runs as cleanup after the test.
Click to reveal answer
What does the @pytest.fixture decorator do?
ARuns a test function multiple times
BSkips a test function
CMarks a function as a reusable setup for tests
DDefines a test case
✗ Incorrect
The @pytest.fixture decorator marks a function as a fixture to provide setup or data for tests.
How do you use a fixture named db in a test?
AAdd <code>db</code> as a parameter to the test function
BUse <code>@db</code> decorator on the test
CImport <code>db</code> in the test file
DCall <code>db()</code> inside the test
✗ Incorrect
You add the fixture name as a parameter to the test function, and pytest injects the fixture's return value.
What is the default scope of a pytest fixture?
Aclass
Bmodule
Csession
Dfunction
✗ Incorrect
The default scope is 'function', meaning the fixture runs once per test function.
How can you perform cleanup actions after a test using a fixture?
AAdd cleanup code after the test function
BUse <code>yield</code> in the fixture and put cleanup code after it
CCall a cleanup function manually in the test
DUse <code>@pytest.cleanup</code> decorator
✗ Incorrect
Using yield in a fixture allows code after yield to run as cleanup after the test.
Which of these is NOT a valid fixture scope?
Athread
Bmodule
Csession
Dfunction
✗ Incorrect
'thread' is not a valid pytest fixture scope.
Explain how the @pytest.fixture decorator helps in writing tests.
Think about how you prepare things before a test runs.
You got /5 concepts.
Describe how to manage resource cleanup using a pytest fixture.
Consider how to run code after the test finishes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @pytest.fixture decorator in pytest?
easy
A. To define a test case with parameters
B. To mark a test as expected to fail
C. To create reusable setup code that can be shared across multiple tests
D. To skip a test during execution
Solution
Step 1: Understand what @pytest.fixture does
The @pytest.fixture decorator is used to create setup code that can be reused in many tests to avoid repetition.
Step 2: Compare options with the fixture purpose
Options A, B, and C describe other pytest features, not fixtures.
Final Answer:
To create reusable setup code that can be shared across multiple tests -> Option C
Quick Check:
Fixture = reusable setup code [OK]
Hint: Fixtures provide shared setup for tests to avoid repetition [OK]
Common Mistakes:
Confusing fixtures with test skipping or marking
Thinking fixtures run tests themselves
Believing fixtures are only for cleanup
2. Which of the following is the correct syntax to define a fixture named db_connection in pytest?
easy
A. def db_connection(): pass
B. @pytest.fixture
def db_connection(): pass
C. @fixture
def db_connection(): pass
D. @pytest.test
def db_connection(): pass
Solution
Step 1: Identify correct decorator usage
The correct decorator for fixtures is @pytest.fixture, fully qualified with the pytest module.
Step 2: Check each option
@pytest.fixture
def db_connection(): pass uses @pytest.fixture correctly. def db_connection(): pass misses the decorator. @fixture
def db_connection(): pass uses @fixture without importing. @pytest.test
def db_connection(): pass uses @pytest.test which is invalid.
Final Answer:
@pytest.fixture
def db_connection(): pass -> Option B
Quick Check:
Use @pytest.fixture before function [OK]
Hint: Always prefix fixture with @pytest.fixture decorator [OK]
Common Mistakes:
Omitting the @pytest.fixture decorator
Using @fixture without importing pytest
Confusing @pytest.test with fixture decorator
3. Given the following pytest code, what will be the output when running the test?