Recall & Review
beginner
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?✗ 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?✗ 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?
✗ 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?
✗ 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?
✗ 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.