Skip to content

Dynamically generate fixtures (again) #6322

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

Closed
Seefooo opened this issue Dec 6, 2019 · 2 comments
Closed

Dynamically generate fixtures (again) #6322

Seefooo opened this issue Dec 6, 2019 · 2 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity

Comments

@Seefooo
Copy link

Seefooo commented Dec 6, 2019

Suggestion/Question:

I am trying to generate dynamic fixtures based on another class that does dependency injection similar to pytest. The dependencies are represented as a dict, which I am hoping to just generate identically named fixtures to make those accessible in test code without having a lot of boilerplate code. I found #2424 (comment) and this works fine for any of the test functions I write, but doesn't work if another fixture has a dependency on one of these "autogenerated" fixtures.

Any advice on how I can expand the above link to support both test cases and fixtures?

@Zac-HD Zac-HD added topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity labels Dec 8, 2019
@Zac-HD
Copy link
Member

Zac-HD commented Dec 29, 2019

Closing this as inactive - maybe try StackOverflow? Sorry we couldn't help 😕

@Zac-HD Zac-HD closed this as completed Dec 29, 2019
@Seefooo
Copy link
Author

Seefooo commented Jan 9, 2020

Sorry, forgot to come back and update this. I figured it out:

@pytest.fixture(autouse=True, scope="session")
def inject_service_fixtures(request):
    try:
        service = request.getfixturevalue("service")
    except FixtureLookupError:
        logger.exception(
            "Service fixture not defined!"
        )
    else:
        our_fixtures = {}
        for name in service.registrations():
            func = partial(getattr, service, name)
            func.__name__ = name
            f = FixtureDef(request._fixturemanager, "", name, func, "function", None)
            our_fixtures[name] = [f]
        request._fixturemanager._arg2fixturedefs.update(our_fixtures)

"service" is the name of our dependency injection object and also has to be a fixture (at session scope). Not ideal I guess, hate to really dig into the internals there, but its definitely been helpful so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants