pytest-asyncio icon indicating copy to clipboard operation
pytest-asyncio copied to clipboard

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

Open av223119 opened this issue 1 year ago • 1 comments

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 avatar May 02 '24 12:05 av223119

Looks like the root cause of this is #706

av223119 avatar May 02 '24 12:05 av223119

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

seifertm avatar Jul 15 '24 13:07 seifertm

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 avatar Aug 26 '24 06:08 seifertm

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 avatar Aug 27 '24 08:08 av223119

@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 avatar Sep 19 '24 04:09 seifertm