python runtime_error ai_generated true

hypothesis.errors.DeadlineExceeded: 测试耗时 412.34ms,超过 200.00ms 的截止时间

hypothesis.errors.DeadlineExceeded: Test took 412.34ms, which exceeds the deadline of 200.00ms

ID: python/pytest-hypothesis-deadline-exceeded

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

版本兼容性

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

根因分析

Hypothesis 对每个示例强制执行截止时间;慢速测试体、冷缓存或 CI 噪声会导致间歇性的 DeadlineExceeded 失败。

English

Hypothesis enforces a per-example deadline; slow test bodies, cold caches, or CI noise cause intermittent DeadlineExceeded failures.

generic

解决方案

  1. 90% 成功率
    from hypothesis import given, settings, strategies as st
    
    @settings(deadline=1000)  # ms
    @given(st.integers())
    def test_parse(n):
        assert parse(n) == n
  2. 85% 成功率
    # Use hypothesis' --hypothesis-show-statistics and cProfile
    pytest --hypothesis-show-statistics tests/test_x.py
    # optimize hot paths in the code under test

无效尝试

常见但无效的做法:

  1. 80% 失败

    Hides real performance regressions and makes flakiness reappear as timeouts elsewhere.

  2. 70% 失败

    Warms caches but doesn't stop Hypothesis from measuring the slow example; may still exceed deadline.

  3. 85% 失败

    Loses the value of property-based testing and may still hit the deadline on the first example.