无法使用 pytest 运行 aiohttp 测试用例

问题描述 投票:0回答:1

我正在尝试在我的 aiohttp 应用程序上运行 pytest。

我添加了一个“pytest.ini”配置文件。

但是每当我运行 pytest 时,它只会给我一个错误。它永远不符合我的测试文件。

我的配置文件如下所示:""" pytest.ini """

[pytest]
addopts = --asyncio

当我与此配置文件一起运行 pytest 时,它不允许运行我的代码并显示此错误:

$ pytest 错误:用法:pytest [选项] [文件或目录] [文件或目录] [...] pytest:错误:无法识别的参数:--asyncio

当我删除配置文件时,它会显示以下内容:

PytestUnhandledCoroutineWarning:异步 def 函数本身不受支持,已被跳过。 您需要为您的异步框架安装合适的插件,例如: - 安尼奥 - pytest异步 - pytest-tornasync - pytest三重奏 - pytest 扭曲 warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))

但是我已经在我的环境中安装了 pytest-asyncio 并且它工作正常。

asynchronous async-await aiohttp pytest-asyncio pytest-aiohttp
1个回答
0
投票

我不确定您为什么决定将

--asyncio
添加到您的选项中(我以前从未见过这样的参数...)。但是,另一个错误看起来只是找不到已安装的插件。

此外,如果我们正在讨论 aiohttp 中的 Web 应用程序,那么您应该按照文档安装 pytest-aiohttp: https://docs.aiohttp.org/en/stable/testing.html

另外,pytest-asyncio 需要一个配置选项来指定异步函数是否应该在没有特殊装饰器的情况下工作:

# pytest.ini
[pytest]
asyncio_mode = auto

https://pytest-asyncio.readthedocs.io/en/latest/reference/configuration.html

© www.soinside.com 2019 - 2024. All rights reserved.