pytest-lazy-fixture icon indicating copy to clipboard operation
pytest-lazy-fixture copied to clipboard

Support callable functions in `lazy_function`

Open youtux opened this issue 2 years ago • 0 comments

This PR adds functionality I'm really missing in this library, the ability to pass callable functions instead of just fixture names. The change is extremely simple, (took mainly from pytest-factoryboy's LazyFixture, which I'm a maintainer).

For example:

@pytest.fixture
def admin_group():
    return Group.objects.get("admin")

@pytest.fixture
def readonly_group():
    return Group.objects.get("readonly")

@pytest.fixture
def user__groups():
    return []  # empty by default

@pytest.fixture
def user(user__groups):
    return User(groups=user__groups)

@pytest.mark.parametrize(
    "user__groups",
    [lazy_fixture(lambda admin_group, readonly_group: [admin_group, readonly_group])]
)
def test_foo(user, admin_group, readonly_group):
    assert set(user.groups.all()) == {admin_group, readonly_group}

If this PR gets merged, I'm planning of removing pytest-factoryboy's own implementation of LazyFixture, since it would be overcome by this library, which implements it in a much more complete way (only the ability of passing callables was missing for me!)

youtux avatar Jul 21 '23 16:07 youtux