pytest asyncio_mode=auto not picking up async tests: how to fix
Gets pytest-asyncio's asyncio_mode=auto actually applied so async test functions are collected and run. Use when async tests are skipped or reported as not run despite the setting. Not for async fixtures misbehaving or for trio/anyio-specific issues.
TL;DR
asyncio_mode=auto is silently ignored when it is set in a config file pytest never loads, or when another async plugin claims the tests first. Verify the setting is in the config file pytest actually reads, confirm pytest-asyncio is installed in the running environment, and check for plugin conflicts. Collection output tells you the truth.
Error
pytest asyncio_mode=auto not picking up async tests: how to fixpytest --co -q lists the async tests as skipped (or omits them) even though asyncio_mode = auto is set.
Steps
- Confirm the plugin is installed where the tests run: pip show pytest-asyncio. Expected output: a version compatible with your pytest version.
- Check which config file pytest loads: run pytest --co -q and read the rootdir and inifile lines at the top. Expected output: the inifile path matches the file where you set asyncio_mode.
- Put the setting under [tool.pytest.inioptions] in pyproject.toml as a quoted string, or under [pytest] in pytest.ini or setup.cfg, exactly asynciomode = auto. Expected output: pytest --co -q collects the async tests instead of skipping them.
- Look for competing plugins: anyio's pytest plugin in auto mode can claim async tests first. Expected output: only one async plugin active, or anyio explicitly scoped.
- Run one async test verbosely to confirm it executed rather than skipped. Expected output: PASSED, not SKIPPED.
When to use
- Async tests show as skipped with asyncio_mode=auto set.
- The setting works in one repo but not another.
- Migrating from explicit pytest.mark.asyncio to auto mode.
When not to use
- Async fixtures error (that is fixture scope config, not collection mode).
- You use anyio or trio deliberately (configure that plugin instead).
- Tests are collected but fail (the mode is working; debug the test).
Variant phrasings
Async tests skipped silently
Skipped with no warning usually means the mode setting never loaded; check inifile first, not the plugin.
event_loop fixture warnings
Newer pytest-asyncio versions changed the event loop fixture; warnings about it are separate from collection mode.
Auto mode works locally but not in CI
CI often installs from a lockfile that pins an older pytest-asyncio, or runs with -p no:cacheprovider masking config; diff the environments.
Why it happens
asyncio_mode is a pytest ini option, so it only takes effect from the ini file pytest selects at startup. A setting in the wrong file, an env without the plugin, or a second async plugin that registers its own collection hook will all silently leave async tests uncollected.
Edge cases
- In pyproject.toml the value must be a quoted string; a bare word is a TOML error, not a pytest error.
- asyncio_mode=auto applies to all async tests including ones you meant to run under anyio; scope with markers if you mix.
- Strict mode plus auto mode in different config files: the last-loaded ini wins, which may not be the file you edited.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_Hg9MXsDwZVmpkIwJgf89mw