【问题标题】:How to test async function using pytest?如何使用 pytest 测试异步功能?
【发布时间】:2021-12-29 02:15:41
【问题描述】:
@pytest.fixture
def d_service():
    c = DService()
    return c

# @pytest.mark.asyncio  # tried it too
async def test_get_file_list(d_service):
    files = await d_service.get_file_list('')
    print(files)

但是,它得到了以下错误?

收集到 0 个项目 / 1 个错误 ==================================== 错误============== ======================= ________________ 错误收集测试/e2e_tests/test_d.py _________________ ..\..\..\..\..\anaconda3\lib\site-packages\pluggy\__init__.py:617: 在 __call__ return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs) ..\..\..\..\..\anaconda3\lib\site-packages\pluggy\__init__.py:222:在 _hookexec return self._inner_hookexec(hook, methods, kwargs) ..\..\..\..\..\anaconda3\lib\site-packages\pluggy\__init__.py:216: 在 firstresult=hook.spec_opts.get('firstresult'), ..\..\..\..\..\anaconda3\lib\site-packages\_pytest\python.py:171:在 pytest_pycollect_makeitem res = 结果.get_result() ..\..\..\..\..\anaconda3\lib\site-packages\anyio\pytest_plugin.py:98:在 pytest_pycollect_makeitem 标记 = collector.get_closest_marker('anyio') E AttributeError:“模块”对象没有属性“get_closest_marker” !!!!!!!!!!!!!!!!!!中断:收集期间出现 1 个错误!!!!!!!!!!!!!!!!!!! ============================ 2.53 秒内出现 1 个错误 ================== =========

我安装了以下软件包。错误消失但测试被跳过。

pip install pytest-asyncio  
(基础)PS>pytest -s 测试\e2e_tests\test_d.py ==================================================== ==================================================== ============== 测试开始 ================================= ==================================================== ================================= 平台 win32 -- Python 3.6.4、pytest-6.2.5、py-1.11.0、pluggy-1.0.0 根目录:C:\Users\X01324908\source\rds\research_data_science\sftp\file_handler 插件:anyio-3.3.4、asyncio-0.16.0 收集了 1 件物品 测试\e2e_tests\test_d.py s ==================================================== ==================================================== ================ 警告总结 ================================ ==================================================== ==================================== 测试/e2e_tests/test_d.py::test_get_file_list c:\users\x01324908\anaconda3\lib\site-packages\_pytest\python.py:172: PytestUnhandledCoroutineWarning: async def 函数不受本机支持,已被跳过。 您需要为您的异步框架安装合适的插件,例如: - 随便 - pytest-asyncio - pytest-tornasync - pytest 三重奏 - pytest 扭曲 warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid))) -- 文档:https://docs.pytest.org/en/stable/warnings.html ==============

【问题讨论】:

  • 你的 pytest 版本是什么?
  • 更新了问题。版本是6.2.5
  • 关于收集错误 - 您的项目布局中一定有一些可疑之处。你能加一个minimal reproducible example吗?除此之外:如果你想用anyio/asyncio 运行测试,你必须用pytest.mark.anyio/pytest.mark.asyncio 标记它们。否则,将跳过异步测试并显示(有点误导)警告 - 这是正确的行为。

标签: python pytest


【解决方案1】:

这对我有用,请尝试:

import asyncio
import pytest

pytest_plugins = ('pytest_asyncio',)

@pytest.mark.asyncio
async def test_simple():
    await asyncio.sleep(0.5)

pytest -v 的输出确认它通过了:

collected 1 item
test_async.py::test_simple PASSED

我已经安装了:

pytest                        6.2.5
pytest-asyncio                0.16.0
# anyio not installed

【讨论】:

  • 如果在await 操作之后还显示一个成功的断言和一个失败的断言,那么测试会更有说服力。
  • 谢谢,我添加了pytest_plugins = ('pytest_asyncio',),它显示通过而不是跳过。
猜你喜欢
  • 2019-11-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
相关资源
最近更新 更多