pytest-asyncio装饰器不生效的根本原因是未启用插件或配置asyncio_mode,且测试函数必须为async def并正确使用@ pytest.mark.asyncio。

pytest-asyncio 装饰器不生效,函数还是被当同步用
根本原因是 pytest-asyncio 默认不自动启用异步支持,必须显式配置或使用装饰器——但仅加 @pytest.mark.asyncio 不够,测试函数本身也得是 async def,且 pytest 运行时需加载插件。
- 确保已安装:
pip install pytest-asyncio - 在
pytest.ini或pyproject.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 场景下极易崩。
立即学习“Python免费学习笔记(深入)”;
-
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 就关早了。这种问题不会报明显错误,只会让后续测试随机失败。










