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()
相关推荐
川石教育5 天前
Pytest中的fixture装饰器详解
python自动化测试·pytest·pytest自动化测试框架·pytest测试框架·pytest单元测试框架
春风又。5 天前
接口自动化——参数化
python·测试工具·自动化·pytest
XTY007 天前
mac电脑pytest生成测试报告
pytest
程序员的世界你不懂7 天前
pytest-前后置及fixture运用
pytest
天才测试猿9 天前
基于Pytest接口自动化的requests模块项目实战以及接口关联方法详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·pytest
HtwHUAT10 天前
五、UI自动化测试05--PyTest框架
经验分享·python·ui·pytest
程序员的世界你不懂10 天前
Pytest-mark使用详解(跳过、标记、参数 化)
pytest
fish_study_csdn12 天前
pytest 技术总结
开发语言·python·pytest
难以怀瑾13 天前
pytest心得体会
pytest
慌糖14 天前
[特殊字符]️ 基于Pytest的自动化测试框架架构解析
pytest