pytest-lazy-fixtures

1.4.0 · active · verified Thu Apr 09

pytest-lazy-fixtures is a pytest plugin that allows the use of fixtures directly within `pytest.mark.parametrize` arguments. It supports injecting fixtures into various data structures and accessing fixture attributes. The library is actively maintained with regular releases and bug fixes, currently at version 1.4.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of `lf` (lazy_fixture) to inject a fixture's return value directly into `pytest.mark.parametrize`. It also shows how to use `lf` with complex data structures like dictionaries and how to access attributes of a fixture's return value using dot notation.

import pytest
from pytest_lazyfixtures import lf

@pytest.fixture()
def my_fixture_value():
    return 'Hello from fixture!'

@pytest.mark.parametrize('param_name', [lf('my_fixture_value')])
def test_example(param_name):
    assert param_name == 'Hello from fixture!'

# Example with a dictionary and attribute access
class MyObject:
    def __init__(self, value):
        self.value = value

@pytest.fixture()
def an_object():
    return MyObject(42)

@pytest.mark.parametrize('data', [
    {'key': lf('my_fixture_value')},
    {'number': lf('an_object.value')}
])
def test_complex_data(data):
    if 'key' in data:
        assert data['key'] == 'Hello from fixture!'
    if 'number' in data:
        assert data['number'] == 42

view raw JSON →