python install_error ai_generated true

不支持原生的 async def 函数。 你需要为异步框架安装合适的插件,例如: - anyio - pytest-asyncio

async def functions are not natively supported. You need to install a suitable plugin for your async framework, for example: - anyio - pytest-asyncio

ID: python/pytest-asyncio-not-installed

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

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

在没有异步插件的情况下,pytest 无法运行 `async def` 测试函数。测试会被收集,但 pytest 拒绝执行,因为没有管理事件循环。

English

Pytest cannot run `async def` test functions without an async plugin. The test is collected but pytest refuses to execute it because the event loop is not managed.

generic

解决方案

  1. 98% 成功率
    Install the plugin and enable auto mode:
    
    pip install pytest-asyncio
    
    # pytest.ini
    [pytest]
    asyncio_mode = auto
  2. 90% 成功率
    Use anyio instead:
    
    pip install anyio
    
    # test file
    import pytest
    
    @pytest.mark.anyio
    async def test_thing():
        ...

无效尝试

常见但无效的做法:

  1. 90% 失败

    The test function itself is still async, so pytest still rejects it during collection.

  2. 95% 失败

    The setting is only read by pytest-asyncio; without the plugin installed the option is ignored and the error persists.