Description
This warning was recently added in #510
I am getting this warning with the following test case. Essentially, we have two directories with async tests. One directory defines a session-scoped event_loop, and the other one doesn't do anything with event_loop.
File tests/integration/conftest.py
:
import asyncio
from typing import Iterator
import pytest
# override event_loop fixture from pytest-asyncio so it has session scope. see:
# https://pytest-asyncio.readthedocs.io/en/latest/reference/fixtures.html
@pytest.fixture(scope="session")
def event_loop() -> Iterator[asyncio.AbstractEventLoop]:
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
File tests/integration/test_integration.py
:
async def test_integration() -> None:
pass
File tests/unit/test_unit.py
:
async def test_unit() -> None:
pass
Also set the pytest configuration in pyproject.toml
as follows:
[tool.pytest.ini_options]
asyncio_mode = "auto"
Now run pytest
from the root directory:
% pytest
====================================================================================================================================== test session starts =======================================================================================================================================
platform darwin -- Python 3.11.1, pytest-7.2.2, pluggy-1.0.0
rootdir: /Users/james.johnston/Thumbtack/postgres-schema/docker/migrate, configfile: pyproject.toml
plugins: asyncio-0.21.0
asyncio: mode=Mode.AUTO
collected 2 items
tests/integration/test_integration.py . [ 50%]
tests/unit/test_unit.py . [100%]
======================================================================================================================================== warnings summary ========================================================================================================================================
tests/unit/test_unit.py::test_unit
/Users/james.johnston/Library/Caches/pypoetry/virtualenvs/ttmigrate-E386TQsv-py3.11/lib/python3.11/site-packages/pytest_asyncio/plugin.py:444: DeprecationWarning: pytest-asyncio detected an unclosed event loop when tearing down the event_loop
fixture: <_UnixSelectorEventLoop running=False closed=False debug=False>
pytest-asyncio will close the event loop for you, but future versions of the
library will no longer do so. In order to ensure compatibility with future
versions, please make sure that:
1. Any custom "event_loop" fixture properly closes the loop after yielding it
2. Your code does not modify the event loop in async fixtures or tests
warnings.warn(
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================================================================================== 2 passed, 1 warning in 0.03s ==================================================================================================================================
(ttmigrate-py3.11) james.johnston@james migrate %
I found that if I move the event_loop fixture to a root tests/conftest.py
, then I no longer get the warning. I am not sure if that is an intentional limitation (or feature?), or if it is a bug.
If the limitation is intentional, may I suggest a documentation improvement? The section at https://pytest-asyncio.readthedocs.io/en/latest/reference/fixtures.html#event-loop talks at length about making your own session-scoped event_loop fixture. But it does not talk about this issue, and it was a little time-consuming to track down in my project.