You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pytest运行aiohttp测试用例失败问题求助

解决pytest运行aiohttp测试的配置问题

1. 删除无效的--asyncio启动参数

你在pytest.ini中配置的--asyncio参数是无效的,pytest-asyncio插件不需要这个启动参数。修改后的pytest.ini如下:

[pytest]
# 移除原有的addopts = --asyncio配置

2. 给异步测试函数添加标记

即使安装了pytest-asyncio,必须给所有async def定义的测试函数加上@pytest.mark.asyncio装饰器,否则pytest会跳过这些测试并抛出警告:

import pytest
from aiohttp import web

@pytest.mark.asyncio
async def test_aiohttp_hello(aiohttp_client):
    app = web.Application()
    app.add_routes([web.get('/', lambda req: web.Response(text='hello'))])
    client = await aiohttp_client(app)
    resp = await client.get('/')
    assert resp.status == 200
    assert await resp.text() == 'hello'

3. 验证插件是否被正确加载

如果仍出现警告,检查当前环境的pytest是否识别到pytest-asyncio插件:
执行以下命令:

pytest --trace-config | grep pytest-asyncio

如果无输出,说明插件未在当前运行环境中,重新安装插件:

pip install pytest-asyncio --upgrade

注意确保运行pytest的虚拟环境和安装插件的环境一致。

4. 升级依赖保证版本兼容

若使用较新版本的pytest(v7.0+),需确保pytest-asyncio版本与之匹配,建议直接升级所有相关依赖:

pip install pytest pytest-asyncio aiohttp --upgrade

内容的提问来源于stack exchange,提问作者protoak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:49:55