python system_error ai_generated partial

Failed: 超时 >30.0s

Failed: Timeout >30.0s

ID: python/pytest-timeout-failed

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

版本兼容性

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

根因分析

pytest-timeout 中止超过配置超时的测试。常见原因:阻塞的网络调用、锁死锁、或未被 mock 的 sleep。

English

pytest-timeout aborts a test that exceeds its configured timeout. Often caused by a blocking network call, a deadlock on a lock, or a sleep not being mocked.

generic

解决方案

  1. 92% 成功率
    from unittest.mock import patch
    
    @patch('app.client.requests.get')
    def test_fetch(mock_get):
        mock_get.return_value.json.return_value = {'ok': True}
        assert fetch()['ok'] is True
  2. 85% 成功率
    # pyproject.toml
    [tool.pytest.ini_options]
    timeout = 30
    timeout_method = "thread"
    
    # on failure a stack trace of the hung thread is printed

无效尝试

常见但无效的做法:

  1. 75% 失败

    Masks the real issue; the underlying hang will still occur and slow CI.

  2. 80% 失败

    Removes the safety net; a truly hung test blocks the whole run until the CI job times out.