-
Using: pytest v8.2.0 pytest-asyncio v0.23.6 I have a fixture that stands up an asyncio subprocesses which my tests connect to over TCP. Re-instantiating this subprocesses for every test is time-consuming and unnecessary. I'd like for my subprocesses fixture to have a module scope. However, I am running into problems because some of my other fixtures are function scoped and I end up with different event loops running for the same test. Here is a minimal example: conftest.py from asyncio.events import AbstractEventLoop
from collections.abc import AsyncGenerator
import pytest_asyncio
import asyncio
@pytest_asyncio.fixture(name="module_scoped", scope="module")
async def fixture_module_scoped() -> AsyncGenerator[AbstractEventLoop, None]:
loop = asyncio.get_running_loop()
print("module_scoped", id(loop))
yield loop
@pytest_asyncio.fixture(name="function_scoped")
async def fixture_function_scoped() -> AsyncGenerator[AbstractEventLoop, None]:
loop = asyncio.get_running_loop()
print("function_scoped", id(loop))
yield loop test_fixtures.py from asyncio import AbstractEventLoop
import asyncio
import pytest
@pytest.mark.asyncio(scope="module")
async def test_fixtures(
function_scoped: AbstractEventLoop,
module_scoped: AbstractEventLoop,
) -> None:
test_loop = asyncio.get_running_loop()
print("test", id(test_loop))
assert test_loop is function_scoped
assert test_loop is module_scoped The last assertion fails:
The IDs printed show that the test, and function scoped fixture, are using the same loop. However, the module scoped fixture has a different loop.
I am wondering how to achieve a single module-scoped event loop for this test. Appreciate any help with this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
My current solution is to pin to pytest-asyncio v0.21.1 and override the event loop fixture to have module scope. |
Beta Was this translation helpful? Give feedback.
-
Pytest-asyncio v0.23 wrongly assumes that the fixture caching scope (defined with Your solution of staying on pytest-asycnio v0.21 is the right choice in your case. |
Beta Was this translation helpful? Give feedback.
Pytest-asyncio v0.23 wrongly assumes that the fixture caching scope (defined with
scope=…
) is the same as the event loop scope in which the async fixture should run. Your example requires both fixtures and the test to run in a module-scope event loop, butfixture_function_scoped
should be reevaluated for every function. This is currently unavailable, but is hopefully fixed in the next version. See #871Your solution of staying on pytest-asycnio v0.21 is the right choice in your case.