0
0
PyTesttesting~5 mins

@pytest.fixture decorator - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ARuns a test function multiple times
BSkips a test function
CMarks a function as a reusable setup for tests
DDefines a test case
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
What is the default scope of a pytest fixture?
Aclass
Bmodule
Csession
Dfunction
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
Which of these is NOT a valid fixture scope?
Athread
Bmodule
Csession
Dfunction
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.