Open
Description
Hi!
In a PR for a project I'm working on, an async test method gets skipped with a PytestUnhandledCoroutineWarning
even though pytest-asyncio
is installed and asyncio_mode
is auto
. I poked at it for while and found that this is because the method was decorated with @unittest.mock.patch.dict(...)
. Here's a minimal reproducer which exposes the issue with Python 3.10.6, pytest-7.1.2, pytest-asyncio-0.19.0:
--- 8< --- test_the_things.py ---
from unittest import mock
import pytest
bar = {}
async def test_runs():
pass
@mock.patch.dict("test_the_things.bar")
async def test_doesnt_run():
pass
@pytest.mark.asyncio
@mock.patch.dict("test_the_things.bar")
async def test_runs_anyway():
pass
--- >8 ---
Here's the output I get:
(pytest-asyncio-issue) nils@makake:~/test/python/pytest-asyncio> pytest --asyncio-mode=auto -v
======================================== test session starts =========================================
platform linux -- Python 3.10.6, pytest-7.1.2, pluggy-1.0.0 -- /home/nils/.virtualenvs/pytest-asyncio-issue/bin/python
cachedir: .pytest_cache
rootdir: /home/nils/test/python/pytest-asyncio
plugins: asyncio-0.19.0
asyncio: mode=auto
collected 3 items
test_the_things.py::test_runs PASSED [ 33%]
test_the_things.py::test_doesnt_run SKIPPED (async def function and no async plugin instal...) [ 66%]
test_the_things.py::test_runs_anyway PASSED [100%]
========================================== warnings summary ==========================================
test_the_things.py::test_doesnt_run
/home/nils/.virtualenvs/pytest-asyncio-issue/lib/python3.10/site-packages/_pytest/python.py:181: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
You need to install a suitable plugin for your async framework, for example:
- anyio
- pytest-asyncio
- pytest-tornasync
- pytest-trio
- pytest-twisted
warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================== 2 passed, 1 skipped, 1 warning in 0.01s ===============================
sys:1: RuntimeWarning: coroutine 'test_doesnt_run' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
(pytest-asyncio-issue) nils@makake:~/test/python/pytest-asyncio>
Either without the decorator or by explicitly adding @pytest.mark.asyncio
the async test functions don't get skipped.