Closed
Description
I have followed instructions from https://pytest-asyncio.readthedocs.io/en/latest/how-to-guides/run_session_tests_in_same_loop.html to add the pytest.mark.asyncio(scope="session")
to all my async tests.
The problem I'm having is that, when these tests contain an async session-scoped fixture, the tests fail with RunTimeError: Event loop is closed
.
If I manually add the @pytest.mark.asyncio(scope="session")
mark on my tests (one by one, without using the pytest hook) then it works fine.
This reproduces the issue. Using pytest-asyncio==0.23.2
and pytest==7.4.3
on python 3.11.16
. I also have asyncio_mode=auto
in case it's relevant
conftest.py
import pytest
from pytest_asyncio import is_async_test
def pytest_collection_modifyitems(items):
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker)
test_hello.py
import pytest
import asyncio
@pytest.fixture
def get_marks(request):
marks = [repr(m) for m in request.node.iter_markers()]
if request.node.parent:
marks += [repr(m) for m in request.node.parent.iter_markers()]
yield marks
@pytest.fixture(scope="session")
async def myfixture():
await asyncio.sleep(0.1)
yield 5
async def test_one(get_marks, myfixture):
print(get_marks)
await asyncio.sleep(0.1)
assert True
async def test_two(get_marks, myfixture):
print(get_marks)
await asyncio.sleep(0.1)
assert True