如何调整Hypothesis中Flakiness的重试次数?
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

