Skip to content

Conversation

@youtux
Copy link

@youtux youtux commented Jul 21, 2023

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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant