You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Hypothesis中Flakiness的重试次数?

Can I adjust Hypothesis's retry count for flaky tests?

Absolutely! You can totally tweak how many times Hypothesis retries a test that fails on its first execution but passes on subsequent runs. Here's how to do it:

Per-test configuration

Use Hypothesis's @settings decorator to set the retries parameter directly on individual tests. This lets you customize behavior for specific flaky tests without affecting others.

Example: Increase retries to 5

from hypothesis import given, settings
from hypothesis.strategies import integers

@given(integers())
@settings(retries=5)
def test_my_flaky_calculation(x):
    # Your test logic here (e.g., a function that occasionally fails)
    assert x + 1 > x  # Trivial example, replace with your actual test

Example: Disable retries entirely

If you don't want Hypothesis to retry a test at all, set retries=0:

@given(integers())
@settings(retries=0)
def test_no_retry_allowed(x):
    assert x > 0

Global configuration

If you want to apply the same retry count to all your Hypothesis tests, you can register a custom settings profile and load it:

from hypothesis import settings

# Create a profile with your desired retry count
settings.register_profile("custom_retries", retries=4)
# Load the profile to apply it globally
settings.load_profile("custom_retries")

When Hypothesis detects a flaky test (fails first run, passes on retries), you'll still see that familiar message:

Flaky: Hypothesis ... produces unreliable results: Falsified on the first call but did not on a subsequent one

This adjustment works exactly as you'd expect—changing the retries value controls how many times Hypothesis will re-run the failed test before marking it as flaky.

内容的提问来源于stack exchange,提问作者curious_george

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:03:51