pytest-asyncio 0.23 doesn't allow class-scoped fixtures outside of class definition
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
Looks like the root cause of this is #706
It's unfortunate that you need boilerplate code to work around this. I expect the issue to resolve itself with #871 .
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?
Sorry, we've switched to alt-pytest-asyncio, so I've lost track of this.
On Mon, 26 Aug 2024 at 08:08, Michael Seifert @.***> wrote:
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 https://github.com/av223119 Does the separation of fixture caching and event loop scopes address your issue?
— Reply to this email directly, view it on GitHub https://github.com/pytest-dev/pytest-asyncio/issues/829#issuecomment-2309395669, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJTUOKJ4NI7JEC2SI4W6DFLZTLA6HAVCNFSM6AAAAABHDSQYVWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMBZGM4TKNRWHE . You are receiving this because you were mentioned.Message ID: @.***>
@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.