python runtime_error ai_generated true

Failed: 来自 pytest-timeout 的超时(>60.0s)。

Failed: Timeout (>60.0s) from pytest-timeout.

ID: python/pytest-timeout-hanging-test

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

版本兼容性

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

根因分析

测试阻塞超过配置的超时时间,通常由网络调用、死锁或未关闭的资源等待输入引起。

English

A test blocks longer than the configured timeout, often due to a network call, deadlock, or an unclosed resource waiting on input.

generic

解决方案

  1. 90% 成功率
    from unittest import mock
    
    def test_fetch(monkeypatch):
        monkeypatch.setattr('myapp.client.requests.get', lambda *a, **k: FakeResp())
        assert myapp.client.fetch()
  2. 85% 成功率
    # pytest.ini
    [pytest]
    timeout = 60
    timeout_method = signal
    # on failure, pytest prints the stack where the test hung

无效尝试

常见但无效的做法:

  1. 85% 失败

    Hides the underlying hang; CI duration balloons and the real bug (deadlock, missing mock) persists.

  2. 95% 失败

    Test suite hangs indefinitely in CI, blocking the whole pipeline until the job-level timeout.

  3. 90% 失败

    Loses coverage and leaves the bug in production code undetected.