Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped

I'm building a py project using poetry I have created a test file and using following code from examples to test asynchronously


import httpx
import respx


@respx.mock
async def test_async_decorator():
    async with httpx.AsyncClient() as client:
        route = respx.get("https://example.org/")
        response = await client.get("https://example.org/")
        assert route.called
        assert response.status_code == 200

When I run poetry run pytest or simply pytest, I'm getting following warning


test_gsx.py::test_async_decorator
  /Users/krishna/Library/Caches/pypoetry/virtualenvs/geoserverx-Yc0Bl2cH-py3.11/lib/python3.11/site-packages/_pytest/python.py:183: 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

my pyproject.toml file has following


[tool.poetry.group.dev.dependencies]
pytest = "^7.1.2"
respx = "^0.20.1"
mypy = "^0.960"
black = "^22.3.0"
isort = "^5.10.1"
pytest-asyncio = "^0.21.0"
anyio = {extras = ["trio"], version = "^3.3.4"}
like image 971
krishna lodha Avatar asked Oct 20 '25 08:10

krishna lodha


2 Answers

You need to add the @pytest.mark.asyncio marker to your test.

Another option would be to make pytest-asyncio work in auto mode so you don't have to mark every test. The way to do it would be (in your toml file):

[tool.pytest.ini_options]
asyncio_mode = "auto"
like image 128
Peter K Avatar answered Oct 22 '25 03:10

Peter K


This also happens if you have a stray yield in your async def test_xxx(): function. Gets me every time.

like image 26
samv Avatar answered Oct 22 '25 04:10

samv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!