pytest asyncio 支持插件 pytest-asyncio

pytest 是 Python 测试框架,但其不支持基于 asyncio 的异步程序(例如,测试 FastAPI 异步代码),pytest-asyncio 是一个 pytest 插件,该插件赋予 pytest 可以测试使用 asyncio 库代码的能力。
https://github.com/pytest-dev/pytest-asyncio

python 复制代码
@pytest.mark.asyncio
async def test_some_asyncio_code():
    res = await library.do_something()
    assert b"expected result" == res

异步 fixture

python 复制代码
import asyncio

import pytest

import pytest_asyncio


@pytest_asyncio.fixture
async def current_loop():
    return asyncio.get_running_loop()

默认事件循环范围是函数范围。可能的循环范围包括 session、package、module、class 和 function。

python 复制代码
import asyncio

import pytest

import pytest_asyncio


@pytest_asyncio.fixture(loop_scope="module")
async def current_loop():
    return asyncio.get_running_loop()


@pytest.mark.asyncio(loop_scope="module")
async def test_runs_in_module_loop(current_loop):
    assert current_loop is asyncio.get_running_loop()
相关推荐
给你一页白纸15 小时前
Pytest 测试用例自动生成:接口自动化进阶实践
python·pytest·接口自动化
工会主席-阿冰18 小时前
使用pytest-selenium插件,ui自动化示例
selenium·pytest
工会主席-阿冰19 小时前
pytest,ui自动化示例
pytest·ui自动化
我一定会有钱3 天前
pytest基础
python·测试工具·测试用例·pytest
西游音月3 天前
(6)pytest+Selenium自动化测试-测试用例编写
selenium·测试用例·pytest
兴趣使然黄小黄5 天前
【Pytest】Pytest框架快速入门
python·pytest
m0_632482506 天前
Jenkins + Pytest +allure接口自动化测试配置与操作
jenkins·集成测试·pytest·jenkins配置
李星星BruceL6 天前
Pytest第三章(参考指南1)
python·自动化·pytest
哎呀呦呵6 天前
pytest基本使用
python·pytest
Kristen_YXQDN6 天前
PyCharm 中 pytest 运行 python 测试文件报错:D:\Python_file\.venv\Scripts\python.exe: No module named pytest
运维·开发语言·python·pycharm·pytest