python runtime_error ai_generated true

RuntimeWarning: coroutine 'test_async_func' was never awaited

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-04-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.21 active

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 80% fail

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