Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
Lazy fixtures run only when a test function includes them as parameters.
Which decorator is used to create a lazy fixture in pytest?
A@pytest.fixture
B@pytest.test
C@pytest.lazy
D@pytest.setup
✗ Incorrect
The @pytest.fixture decorator creates fixtures, which are lazy by default unless autouse=True.
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
✗ Incorrect
Fixtures with autouse=True run automatically before each test, so they are not lazy.
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
✗ Incorrect
Lazy fixtures save time by running only when a test actually uses them.
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
✗ Incorrect
Fixtures can depend on other fixtures, and those dependencies are lazy too.
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.
Practice
(1/5)
1. What is the main benefit of using lazy_fixture in pytest tests?
easy
A. It automatically retries failed tests using the fixture.
B. It runs all fixtures before any test starts, ensuring setup order.
C. It delays fixture setup until the test actually needs it, improving speed.
D. It converts fixtures into global variables accessible everywhere.
Solution
Step 1: Understand lazy_fixture purpose
The lazy_fixture delays the setup of a fixture until the test that uses it actually runs, avoiding unnecessary setup.
Step 2: Compare options to this behavior
Only It delays fixture setup until the test actually needs it, improving speed. correctly describes this benefit. Other options describe unrelated behaviors.
Final Answer:
It delays fixture setup until the test actually needs it, improving speed. -> Option C
Quick Check:
lazy_fixture delays setup = A [OK]
Hint: Lazy fixtures delay setup until needed, saving time [OK]
Common Mistakes:
Thinking lazy_fixture runs all fixtures upfront
Confusing lazy_fixture with test retries
Assuming lazy_fixture makes fixtures global
2. Which of the following is the correct way to use lazy_fixture inside pytest.mark.parametrize?
easy
A. pytest.mark.parametrize('data', lazy_fixture('my_fixture'))
B. pytest.mark.parametrize('data', lazy_fixture(['my_fixture']))
C. pytest.mark.parametrize('data', ['lazy_fixture(my_fixture)'])
D. pytest.mark.parametrize('data', [lazy_fixture('my_fixture')])
Solution
Step 1: Recall correct syntax for lazy_fixture usage
The lazy_fixture function must be called inside the list of parameters passed to pytest.mark.parametrize, like [lazy_fixture('fixture_name')].
Step 2: Check each option's syntax
pytest.mark.parametrize('data', [lazy_fixture('my_fixture')]) correctly wraps lazy_fixture('my_fixture') inside a list. Options A and D misuse the function call or argument types. pytest.mark.parametrize('data', ['lazy_fixture(my_fixture)']) treats it as a string, which is incorrect.
Final Answer:
pytest.mark.parametrize('data', [lazy_fixture('my_fixture')]) -> Option D
Quick Check:
lazy_fixture inside list = B [OK]
Hint: Use lazy_fixture inside a list in parametrize [OK]
Common Mistakes:
Passing lazy_fixture call directly without list
Using string quotes around lazy_fixture call
Passing list inside lazy_fixture instead of outside
3. Given the code below, what will be the output when running the test?
A. The test function must not use parameter named 'input'.
B. lazy_fixture must be inside a list in parametrize, not passed directly.
C. Fixture 'data' must return a single integer, not a list.
D. lazy_fixture cannot be used with parametrize.
Solution
Step 1: Check lazy_fixture usage in parametrize
The lazy_fixture call must be inside a list when passed to pytest.mark.parametrize. Here it is passed directly, which is incorrect syntax.
Step 2: Validate other code parts
The fixture returns a list correctly, parameter name 'input' is allowed, and lazy_fixture is designed for parametrize usage.
Final Answer:
lazy_fixture must be inside a list in parametrize, not passed directly. -> Option B
Quick Check:
lazy_fixture inside list required = C [OK]
Hint: Wrap lazy_fixture call in a list for parametrize [OK]
Common Mistakes:
Passing lazy_fixture call directly without list
Misunderstanding fixture return types
Thinking parameter names are restricted
5. You want to test a function with two different fixtures, but only one fixture should be set up per test run to save time. How can you use lazy_fixture with pytest.mark.parametrize to achieve this?
hard
A. Use pytest.mark.parametrize('arg', [lazy_fixture('fix1'), lazy_fixture('fix2')]) so only the needed fixture runs per test.
B. Call both fixtures inside the test and use lazy_fixture to delay both setups.
C. Use pytest.mark.parametrize with a list of fixture names as strings, then call them inside the test.
D. Set both fixtures as autouse=True to run only one at a time.
Solution
Step 1: Understand lazy_fixture with parametrize
Using lazy_fixture inside the list passed to pytest.mark.parametrize allows pytest to run only the fixture needed for each test case.
Step 2: Evaluate options for efficiency
Use pytest.mark.parametrize('arg', [lazy_fixture('fix1'), lazy_fixture('fix2')]) so only the needed fixture runs per test. correctly uses lazy_fixture in the parametrize list, so only one fixture runs per test. Call both fixtures inside the test and use lazy_fixture to delay both setups. runs both fixtures regardless. Use pytest.mark.parametrize with a list of fixture names as strings, then call them inside the test. uses strings incorrectly. Set both fixtures as autouse=True to run only one at a time. misuses autouse and does not control fixture setup per test.
Final Answer:
Use pytest.mark.parametrize('arg', [lazy_fixture('fix1'), lazy_fixture('fix2')]) so only the needed fixture runs per test. -> Option A
Quick Check:
lazy_fixture in parametrize list runs one fixture per test = D [OK]
Hint: Parametrize with lazy_fixture list to run one fixture per test [OK]
Common Mistakes:
Calling both fixtures inside test causing both setups
Passing fixture names as strings without lazy_fixture