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

pytest-asyncio 0.23 doesn't allow class-scoped fixtures outside of class definition #829

Closed
av223119 opened this issue May 2, 2024 · 5 comments
Labels
bug needsinfo Requires additional information from the issue author
Milestone

Comments

@av223119
Copy link

av223119 commented May 2, 2024

It appears that the pytest-asyncio 0.23 broke class-scoped fixtures defined outside of the class. Namely, I used to have the following setup:

conftest.py:

@pytest_asyncio.fixture(scope="class")
async def run_important_program_in_class() -> AsyncGenerator[ImportantProgram, None]:
    """runs one Program instance per test class"""
    async with run_important_program(cfg=generate_cfg()) as p:
        yield p

and then the test classes used
@pytest.mark.usefixtures("run_important_program_in_class")

however with upgrade to 0.23, now the following error being reported:

ERROR my_example_test.py - _pytest.config.exceptions.UsageError: my_example_test.py is marked to be run i
n an event loop with scope class, but is not part of any class

The error disappears of the fixture is moved to the class definition, like this:

Class TestSomeStuff:
    @pytest_asyncio.fixture(scope="class")
    async def run_important_program_in_class() -> AsyncGenerator[ImportantProgram, None]:
        async with run_important_program(cfg=generate_cfg()) as p:
            yield p

But this approach leads to significant code duplication

@av223119
Copy link
Author

av223119 commented May 2, 2024

Looks like the root cause of this is #706

@seifertm
Copy link
Contributor

It's unfortunate that you need boilerplate code to work around this. I expect the issue to resolve itself with #871 .

@seifertm
Copy link
Contributor

As of pytest-asyncio v0.24 async fixtures can have different scopes for caching (scope=…) and for the event loop used to run the fixture (loop_scope=…). It's still not possible to use loop_scope="class" if the fixture is not surrounded by a class, though.

@av223119 Does the separation of fixture caching and event loop scopes address your issue?

@seifertm seifertm added the needsinfo Requires additional information from the issue author label Aug 26, 2024
@av223119
Copy link
Author

av223119 commented Aug 27, 2024 via email

@seifertm
Copy link
Contributor

@av223119 Thanks for the response.

In my opinion, the changes in v0.24 are enough to address this. Therefore, I'll close the issue.

Feel free to continue the discussion if you disagree.

@seifertm seifertm closed this as not planned Won't fix, can't repro, duplicate, stale Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needsinfo Requires additional information from the issue author
Projects
None yet
Development

No branches or pull requests

2 participants