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()
相关推荐
测试的菜鸟6 小时前
[pytest] 配置
python·pytest
互联网杂货铺20 小时前
python+pytest 接口自动化测试:参数关联
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
挽风8212 天前
Pytest自动化框架
自动化·pytest·模块测试
Xiaoshuang_Cao2 天前
pytest结合allure
pytest
摸鱼仙人~3 天前
结合unittest和pytest进行虚拟数据库测试
数据库·windows·pytest
江梦寻5 天前
如何使用 Python+Flask+win32print 实现简易网络打印服务
开发语言·后端·python·flask·pytest·web3.py·win32
qq_白羊座5 天前
pytest框架 核心知识的系统复习
pytest
@TangXin6 天前
单元测试-pytest框架实践
自动化测试·单元测试·pytest
evelol76 天前
【pytest框架源码分析四】pluggy源码分析之hook执行
自动化·pytest
云泽野8 天前
Pytest之parametrize参数化
android·python·pytest