pytest 'async def functions are not natively supported' with pytest-asyncio 0.23
The tests didn't fail. They just stopped running, which was worse. After upgrading pytest-asyncio from 0.21.1 to 0.23.7, our async test suite went from 180 passing tests to 180 passing tests and a wall of warnings that nobody read:
PytestUnhandledCoroutineWarning: async def functions are not natively supported.
You need to install a suitable plugin for your async framework, for example:
- anyio
- pytest-asyncio
/home/runner/work/api/tests/test_orders.py:42
@pytest.mark.asyncio
PytestUnhandledCoroutineWarning: async def functions are not natively supported
CI stayed green because pytest doesn't count warnings as failures. We noticed two weeks later when a bug shipped that had a test which should have caught it.
## What changed in 0.23
pytest-asyncio 0.21 introduced asyncio_mode and made strict the default in 0.23. In strict mode, an async test only runs if it's decorated with @pytest.mark.asyncio. In the old auto behavior, any async def test_* was picked up automatically. Our tests had the marker on some functions but not others — the ones without it were the ones silently skipped.
There's also a config-level change: in strict mode the marker must be registered. If you don't register it you get PytestUnknownMarkWarning on top of everything.
## The config that brought them back
In pyproject.toml:
toml
[tool.pytest.ini_options]
asyncio_mode = "strict"
markers = [
"asyncio: marks tests as async",
]
Then add the marker to every async test:
python
import pytest@pytest.mark.asyncio
async def test_creates_order(session):
result = await create_order(session, sku="A-1")
assert result.status == "pending"
If you'd rather not touch 180 test functions, set asyncio_mode = "auto" instead:
toml
[tool.pytest.ini_options]
asyncio_mode = "auto"
That restores the old behavior and the marker becomes optional. We took the strict route because it makes it obvious which tests are async, but auto is a legitimate one-line fix if you're in a hurry.
## The fixture trap
This is the part that cost me an afternoon. Strict mode applies to fixtures too. An async fixture decorated with @pytest.fixture but no @pytest_asyncio.fixture returns a coroutine object, and your test then fails with something like:
TypeError: object async_generator can't be used in 'await' expression
or, more confusingly, AttributeError: 'coroutine' object has no attribute 'fetchone'. Fix it by importing the decorator:
python
import pytest_asyncio@pytest_asyncio.fixture
async def session():
async with engine.begin() as conn:
yield conn
## Make warnings fail in CI
The real lesson is that we shouldn't have been able to ship with 180 skipped coroutines. Add this to pyproject.toml:
toml
filterwarnings = ["error::pytest.PytestUnhandledCoroutineWarning"]
Or just run pytest with -W error::pytest.PytestUnhandledCoroutineWarning in CI. We now run the whole suite with --strict-markers too, which catches typos in marker names.
One more thing worth knowing: --strict-markers will also complain about markers you forgot to list, so once you turn it on, add asyncio to the markers list as shown above. We learned that by breaking a different CI job the day after fixing this one.
If you're on pytest-asyncio 0.24 or later, the behavior is the same — strict is still the default. The version number in the title is just where we hit it.