Skip to content

Event loop is closed when using pytester and pytest_asyncio.fixture(autouse=True) #291

Closed
@unmade

Description

@unmade

If you have an async fixture with a teardown and autouse=True and you have a simple non-async test with a pytester plugin, then that fixture is always added as a finalizer.

Basically, if you have the following:

# tests/conftest.py

@pytest.fixture(autouse=True)
async def teardown(request):
    try:
        yield
    finally:
        pass


# tests/test_fixtures.py

def test_fixture_setup(pytester):
    # create a temporary conftest.py file
    pytester.makeconftest("""
        pytest_plugins = ["tests.conftest"]
    """)

    # create a temporary pytest test file
    pytester.makepyfile("""
        def test_setup():
            assert True
    """)

    result = pytester.runpytest("--asyncio-mode=auto")
    result.assert_outcomes(passed=1)

Then, the test fails on a teardown with the following error:

ERROR tests/test_fixtures.py::test_fixture_setup - RuntimeError: Event loop is closed

I've checked it on pytest-asyncio==0.16.0 and pytest>=0.17.0. However, prior v0.17.0 there was a workaround - you could redefine an event_loop in a temporary pytest test file without closing the loop. Since, v0.17.0 this is no longer possible.
In v0.17.0 the only workaround is to override this async fixture itself in temporary pytest test file.


I made a minimal reproducible example, you can find it here:

https://github.com/unmade/pytester-pytest-asyncio-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions