英文:
PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped
问题
I'm building a py project using poetry
:
我正在使用 poetry
构建一个 Python 项目:
I have created a test file and using the 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 the following warning:
当我运行 poetry run pytest
或者简单运行 pytest
时,我收到以下警告:
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 the following:
我的 pyproject.toml 文件包含以下内容:
[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"}
英文:
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"}
答案1
得分: 1
你需要将 @pytest.mark.asyncio
标记添加到你的测试中。
另一个选择是使 pytest-asyncio
在 auto mode
下工作,这样你就不需要为每个测试添加标记。做法是(在你的 toml 文件中):
[tool.pytest.ini_options]
asyncio_mode = "auto"
英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论