Verify lazy fixture usage in pytest
Preconditions (2)
✅ Expected Result: The test passes successfully, confirming that the lazy fixture was correctly injected and used in parameterized test
Jump into concepts and practice - no test required
import pytest from pytest_lazyfixture import lazy_fixture @pytest.fixture def data(): return {'value': 42} @pytest.mark.parametrize('input_data', [lazy_fixture('data')]) def test_lazy_fixture(input_data): assert input_data['value'] == 42
This test script uses pytest and the pytest-lazy-fixture plugin to demonstrate lazy fixture usage.
First, the data fixture returns a dictionary with a key value set to 42.
The test function test_lazy_fixture is parameterized with lazy_fixture('data'), which means the fixture data is injected lazily as a parameter.
Inside the test, we assert that the dictionary passed has the key value equal to 42, confirming the fixture was correctly used.
This approach allows fixtures to be used inside parameterized tests, improving test flexibility and reuse.
Extend the test to use three different fixtures with different data dictionaries and verify their values
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.