登录
首页 >  文章 >  python教程

Python异步测试怎么写?pytest-asyncio插件使用教程

时间:2026-03-31 21:07:54 378浏览 收藏

Python异步测试看似只需加`async def`和`@pytest.mark.asyncio`,实则暗藏重重陷阱:装饰器失效、协程未执行、event loop冲突、资源泄漏、mock失灵、fixture清理错乱……根本原因在于pytest-asyncio并非开箱即用,必须显式启用`asyncio_mode`、严格匹配异步函数签名、杜绝手动调用`asyncio.run()`,并用专为异步设计的fixture机制管理生命周期——真正难点不在语法,而在于精准掌控事件循环的创建、复用与销毁时机,稍有不慎就会引发静默失败或随机崩溃。

Python异步测试怎么写_pytest-asyncio插件装饰器@pytest.mark.asyncio测试协程函数

pytest-asyncio 装饰器不生效,函数还是被当同步用

根本原因是 pytest-asyncio 默认不自动启用异步支持,必须显式配置或使用装饰器——但仅加 @pytest.mark.asyncio 不够,测试函数本身也得是 async def,且 pytest 运行时需加载插件。

  • 确保已安装: pip install pytest-asyncio
  • pytest.inipyproject.toml 中启用插件(否则装饰器会被忽略):
    [tool:pytest]
    asyncio_mode = auto
    [tool.pytest.ini_options]
    asyncio_mode = "auto"
  • @pytest.mark.asyncio 只能修饰 async def 函数,修饰普通 def 会静默失败,pytest 报告里仍显示“passed”,但实际没 await
  • 别在类方法上只加装饰器不配 asyncio_mode:类内 async def test_xxx 仍需全局配置,否则被跳过或报 RuntimeWarning: coroutine 'xxx' was never awaited

测试里 await 一个协程,结果报 RuntimeError: no running event loop

这是 pytest 在非 asyncio 环境下直接调用协程导致的——@pytest.mark.asyncio 的作用就是让 pytest 在专用 event loop 中运行该测试,但如果你手动 await my_coro() 之前又做了其他同步操作(比如提前调用了 asyncio.run()),loop 就可能被关闭或冲突。

  • 绝对不要在测试函数里调用 asyncio.run():它会创建新 loop 并关闭,和 pytest-asyncio 管理的 loop 冲突
  • 所有 await 必须在被 @pytest.mark.asyncio 修饰的 async def 函数体内直接写,不要包进另一个函数再调用
  • 如果被测函数返回协程对象(不是 awaitable),要先 await 它;如果返回的是 Future 或 Task,也要 await,否则 loop 不会推进
  • 常见陷阱:mock 一个协程函数时,用 AsyncMock 替代 Mock,否则 await mock() 会报错

多个 async 测试之间共享状态(比如数据库连接池)出问题

asyncio 的 event loop 是 per-test 隔离的,但像连接池、全局 client 实例这类对象如果在模块级初始化,可能被多个 loop 共享,引发 RuntimeError 或连接泄漏。

  • 避免模块级创建 async client(如 aiohttp.ClientSession()aiomysql.create_pool()):改用 pytest 的 async def pytest_asyncio_fixture(需配 @pytest.fixture + @pytest.mark.asyncio
  • fixture 的 scope="session" 不适用于 async fixture——必须用 scope="function""module",并确保 cleanup 是 async(用 yield + await 关闭)
  • 若用 asyncpg.create_pool(),记得在 fixture teardown 里调用 pool.close()await pool.wait_closed(),否则测试退出时报 warning
  • 别在 setup_method / teardown_method 里做 async 操作:pytest 不支持,要用 fixture 替代

为什么用 pytest-asyncio 而不用 asyncio.run() 包一层

因为 asyncio.run() 每次都新建并关闭 loop,开销大,且无法复用 pytest 的 fixture 生命周期、超时控制、并发执行等能力;更重要的是,它会让 mock 失效、loop 状态混乱,尤其在涉及信号、子进程或嵌套 await 场景下极易崩。

  • pytest-asyncio 复用同一个 loop 实例,支持 --asyncio-mode=strict 检查未 await 的协程
  • 它让 async def test_xxx() 表现得和普通测试一样:可加 @pytest.mark.timeout、可被 pytest-xdist 并发跑(每个 worker 有自己的 loop)
  • 直接套 asyncio.run() 后,mock 的 AsyncMock 调用不会触发 await_count 统计,断言难写
  • 真实项目里,协程常依赖 event loop 上注册的 signal handler 或后台 task,asyncio.run() 无法保留这些上下文

async 测试最难的从来不是语法,而是 loop 生命周期和资源清理的耦合点——比如一个 fixture 初始化了 pool,另一个 fixture 用了它,但 teardown 顺序错了,loop 就关早了。这种问题不会报明显错误,只会让后续测试随机失败。

以上就是《Python异步测试怎么写?pytest-asyncio插件使用教程》的详细内容,更多关于的资料请关注golang学习网公众号!

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>