Skip to content

Commit 39a8f72

Browse files
committed
raise flaky when replaying backend flakes
1 parent 09b00a2 commit 39a8f72

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

hypothesis-python/src/hypothesis/internal/conjecture/engine.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from hypothesis import HealthCheck, Phase, Verbosity, settings as Settings
3939
from hypothesis._settings import local_settings
4040
from hypothesis.database import ExampleDatabase
41-
from hypothesis.errors import InvalidArgument, StopTest
41+
from hypothesis.errors import Flaky, InvalidArgument, StopTest
4242
from hypothesis.internal.cache import LRUReusedCache
4343
from hypothesis.internal.compat import (
4444
NotRequired,
@@ -506,12 +506,20 @@ def test_function(self, data: ConjectureData) -> None:
506506
if self.settings.backend != "hypothesis":
507507
# drive the ir tree through the test function to convert it
508508
# to a buffer
509+
initial_origin = data.interesting_origin
509510
data = ConjectureData.for_ir_tree(data.examples.ir_tree_nodes)
510511
self.__stoppable_test_function(data)
511512
data.freeze()
512-
# should we raise Flaky here instead?
513+
# we'd like to use expected_failure machinery here from
514+
# StateForActualGivenExecution for better diagnostic reports of eg
515+
# flaky deadlines, but we're too low down in the engine for that.
516+
# for now a worse generic flaky error will have to do.
513517
if data.status != Status.INTERESTING:
514-
self.exit_with(ExitReason.flaky)
518+
raise Flaky(
519+
f"Inconsistent results from replaying a failing test case!\n"
520+
f" last: {Status.INTERESTING.name} from {initial_origin}\n"
521+
f" this: {data.status.name}"
522+
)
515523

516524
self._cache(data)
517525

hypothesis-python/tests/conjecture/test_alt_backend.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from hypothesis import given, settings, strategies as st
2020
from hypothesis.database import InMemoryExampleDatabase
21-
from hypothesis.errors import InvalidArgument
21+
from hypothesis.errors import Flaky, InvalidArgument
2222
from hypothesis.internal.compat import int_to_bytes
2323
from hypothesis.internal.conjecture.data import (
2424
AVAILABLE_PROVIDERS,
@@ -354,3 +354,19 @@ def test_function(n):
354354
<= test_case_lifetime_init_count
355355
<= test_function_count + 10
356356
)
357+
358+
359+
def test_flaky_with_backend():
360+
with temp_register_backend("trivial", TrivialProvider):
361+
362+
calls = 0
363+
364+
@given(st.integers())
365+
@settings(backend="trivial", database=None)
366+
def test_function(n):
367+
nonlocal calls
368+
calls += 1
369+
assert n != calls % 2
370+
371+
with pytest.raises(Flaky):
372+
test_function()

0 commit comments

Comments
 (0)