pytest-asyncio
pytest-asyncio copied to clipboard
Async fixtures may break current event loop
It seems pytest-asyncio (0.23.7
) is currently badly broken with code relying on aiohttp
.
This simple example currently breaks. The example is heavily simplified from the actual fixture setup. This used to work fine before 0.22
.
import aiohttp
import pytest
# No longer working for getting a single event loop for aiohttp
# @pytest.fixture(scope="session")
# def event_loop():
# loop = asyncio.get_event_loop_policy().new_event_loop()
# try:
# yield loop
# finally:
# loop.close()
@pytest.fixture(scope="session")
async def client(
# event_loop <- accessing this no longer working. But neither this works without it
):
async with aiohttp.ClientSession(
# loop=event_loop <- aiohttp requires using a single event_loop that can not be closed between the tests...
) as session:
yield session
async def test_the_client(client: aiohttp.ClientSession):
await client.get("http://localhost:8080/foobar") # RuntimeError: Timeout context manager should be used inside a task
Using asyncio_mode = "auto"
:
[tool.poetry]
name = "foobar"
package-mode = false
[tool.poetry.dependencies]
python = "~3.12"
aiohttp = "==3.9.5"
[tool.poetry.dev-dependencies]
pytest = "==8.2.2"
pytest-asyncio = "==0.23.7"
[tool.pytest.ini_options]
asyncio_mode = "auto"