lundberg / respx

Mock HTTPX with awesome request patterns and response side effects 🦋
https://lundberg.github.io/respx
BSD 3-Clause "New" or "Revised" License
581 stars 38 forks source link

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

Closed krishnaglodha closed 12 months ago

krishnaglodha commented 1 year ago

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"}
lundberg commented 1 year ago

This is not respx, but rather how you run/configure pytest to run with asyncio support.

So if you're using pytest-asyncio, make sure you've configured pytest options to set asyncio mode to e.g. auto, or marking the test/module with the pytest-asyncio marker.