Skip to content

Allow broader scoped fixtures to rely on narrower scoped fixtures #797

Closed
@jaraco

Description

@jaraco

I have this scenario: a plugin provides a fixture for a resource:

@pytest.fixture(scope='function')
def cheap_thing():
    return dict(a=1, b=2)

In my application, I want to do some heavy processing on cheap thing and create an expensive thing. Because it's an expensive thing, I want it to be session scoped:

@pytest.fixture(scope='session')
def expensive_thing(cheap_thing):
    return expensive_transformation(cheap_thing)

However, the scoping rules disallow this usage. I can understand how the implementation may have gotten to a place where the scope processing might preclude naturally using a function-scoped fixture in a session-scoped fixture. However, I don't see any reason why the function-scoped fixture couldn't be invoked to be utilized in this way, where the function-scoped fixture is invoked and that instance of the fixture scheduled for teardown at the close of the session scope.

It might be possible to refactor the functionality like so:

@pytest.fixture(scope='session')
def expensive_thing():
    return expensive_transform(cheap_thing())

Except that presumes things about the behavior of cheap_thing. If it were changed to a yield_fixture, for example, that would break the workaround.

Is there something more intrinsic that would preclude the behavior proposed above? Would it add undue complexity?

Thanks for the consideration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions