python runtime_error ai_generated true

RuntimeWarning: 协程 'test_async_func' 从未被等待

RuntimeWarning: coroutine 'test_async_func' was never awaited

ID: python/pytest-asyncio-coroutine-not-awaited

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-04-01首次发现

版本兼容性

版本状态引入弃用备注
0.21 active

根因分析

异步测试函数未被等待,通常是因为测试未标记 @pytest.mark.asyncio 或 asyncio 模式未设置为 auto。

English

An async test function was not awaited, often because the test was not marked with @pytest.mark.asyncio or the asyncio mode is not set to auto.

generic

解决方案

  1. 98% 成功率
    import pytest
    
    @pytest.mark.asyncio
    async def test_async_func():
        result = await some_async_call()
        assert result == expected
  2. 95% 成功率
    [pytest]
    asyncio_mode = auto
    This automatically treats async tests as asyncio tests.

无效尝试

常见但无效的做法:

  1. 90% 失败

    pytest-asyncio handles the event loop; manually running can cause conflicts.

  2. 80% 失败

    This changes the test to synchronous and may not test the intended behavior.