0
0
PyTesttesting~5 mins

Lazy fixtures in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a lazy fixture in pytest?
A lazy fixture is a fixture that is not executed immediately but only when it is actually used in a test. It helps to delay setup until necessary, saving time and resources.
Click to reveal answer
beginner
How do you declare a lazy fixture in pytest?
You declare a lazy fixture by using the @pytest.fixture decorator with scope and optionally autouse=False. The fixture code runs only when a test requests it.
Click to reveal answer
intermediate
Why use lazy fixtures instead of eager fixtures?
Lazy fixtures run only when needed, which saves test execution time and avoids unnecessary setup. Eager fixtures run before every test regardless of need, which can slow tests down.
Click to reveal answer
beginner
What happens if a lazy fixture is not used in any test?
If a lazy fixture is not used by any test, it will not run at all. This means no setup or teardown code inside it will execute, saving resources.
Click to reveal answer
intermediate
Can lazy fixtures depend on other fixtures in pytest?
Yes, lazy fixtures can depend on other fixtures. The dependent fixtures will also be lazy and run only when needed by the test.
Click to reveal answer
What triggers a lazy fixture to run in pytest?
ABefore any test runs automatically
BWhen the fixture is declared
CWhen pytest starts
DWhen a test function requests it as a parameter
Which decorator is used to create a lazy fixture in pytest?
A@pytest.fixture
B@pytest.test
C@pytest.lazy
D@pytest.setup
If a fixture is marked with autouse=True, is it lazy?
ANo, it runs before every test automatically
BNo, it never runs
CYes, but only for some tests
DYes, it runs only when requested
What is a benefit of using lazy fixtures?
AThey run faster because they run before all tests
BThey reduce test time by running only when needed
CThey run multiple times per test
DThey replace test functions
Can a lazy fixture depend on another fixture?
AOnly if both are autouse
BNo, fixtures cannot depend on each other
CYes, dependencies are also lazy
DOnly if declared in the same file
Explain what lazy fixtures are in pytest and why they are useful.
Think about when the fixture code runs during testing.
You got /4 concepts.
    Describe how fixture dependencies work with lazy fixtures in pytest.
    Consider how pytest manages fixture calls.
    You got /4 concepts.