Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support callable functions in lazy_function #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

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.

None yet

1 participant