Test Overview
This test uses a lazy fixture to provide test data only when needed. It verifies that the lazy fixture correctly supplies the expected value during test execution.
Jump into concepts and practice - no test required
This test uses a lazy fixture to provide test data only when needed. It verifies that the lazy fixture correctly supplies the expected value during test execution.
import pytest @pytest.fixture def data(): print("Setup data fixture") return 42 @pytest.mark.parametrize('lazy_fixture', [pytest.lazy_fixture('data')]) def test_lazy_fixture(lazy_fixture): assert lazy_fixture == 42
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and collects tests | pytest collects test_lazy_fixture with lazy_fixture parameter | — | PASS |
| 2 | pytest invokes the 'data' fixture lazily when test_lazy_fixture runs | Fixture 'data' setup prints 'Setup data fixture' and returns 42 | — | PASS |
| 3 | test_lazy_fixture receives lazy_fixture value 42 and runs assertion | Inside test, lazy_fixture == 42 | assert lazy_fixture == 42 | PASS |
| 4 | Test completes successfully | Test passed with no errors | — | PASS |
lazy_fixture in pytest tests?lazy_fixture delays the setup of a fixture until the test that uses it actually runs, avoiding unnecessary setup.lazy_fixture inside pytest.mark.parametrize?lazy_fixture function must be called inside the list of parameters passed to pytest.mark.parametrize, like [lazy_fixture('fixture_name')].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.import pytest
from pytest_lazyfixture import lazy_fixture
@pytest.fixture
def number():
print('Setup number')
return 42
@pytest.mark.parametrize('value', [lazy_fixture('number')])
def test_value(value):
print(f'Test got {value}')
assert value == 42
number is only set up when the test runs because of lazy_fixture. So 'Setup number' prints before the test body.import pytest
from pytest_lazyfixture import lazy_fixture
@pytest.fixture
def data():
return [1, 2, 3]
@pytest.mark.parametrize('input', lazy_fixture('data'))
def test_sum(input):
assert sum(input) == 6
lazy_fixture call must be inside a list when passed to pytest.mark.parametrize. Here it is passed directly, which is incorrect syntax.lazy_fixture with pytest.mark.parametrize to achieve this?lazy_fixture inside the list passed to pytest.mark.parametrize allows pytest to run only the fixture needed for each test case.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.