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()
相关推荐
旦莫2 天前
Pytest教程:Pytest与主流测试框架对比
人工智能·python·pytest
X1A0RAN2 天前
解决docker容器执行pytest命令报socket.gaierror: [Errno -2] Name or service not known 错误问题
docker·容器·pytest
程序员三藏3 天前
接口自动化测试之 pytest 接口关联框架封装
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试
旦莫4 天前
怎么才能算对自动化测试框架二次开发(以Pytest为例)
python·测试开发·pytest·ai测试
彩色面团儿6 天前
Pytest框架测试用例分析(测试笔记二)
笔记·测试用例·pytest
阿蔹6 天前
Python-Pytest-Requests-API接口测试自动化框架2
python·自动化·pytest
彩色面团儿7 天前
Pycharm部署pytest运行测试(测试笔记一)
python·pycharm·集成测试·pytest
阿蔹8 天前
Python-Pytest
python·自动化·pytest
阿蔹8 天前
Python-Pytest-Requests-API接口测试自动化框架
python·自动化·pytest
先做个垃圾出来………8 天前
pytest + pytest-mock + pytest-parametrize为基础构建测试框架
microsoft·pytest