Skip to content

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

Closed as not planned
@av223119

Description

@av223119

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneedsinfoRequires additional information from the issue author

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions