Skip to content

Commit

Permalink
Merge pull request #3186 from touilleMan/issue-3185
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD authored Dec 10, 2021
2 parents af25acd + ecced4c commit 5d79bc0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This patch fix invariants display in stateful falsifying examples (:issue:`3185`).
5 changes: 5 additions & 0 deletions hypothesis-python/src/hypothesis/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ def check_invariants(self, settings):
continue
if not all(precond(self) for precond in invar.preconditions):
continue
if (
current_build_context().is_final
or settings.verbosity >= Verbosity.debug
):
report(f"state.{invar.function.__name__}()")
result = invar.function(self)
if result is not None:
fail_health_check(
Expand Down
86 changes: 86 additions & 0 deletions hypothesis-python/tests/cover/test_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,92 @@ def test_foo(self):
run_state_machine_as_test(BadPrecondition)


def test_invariant_failling_present_in_falsifying_example():
@Settings(print_blob=False)
class BadInvariant(RuleBasedStateMachine):
@initialize()
def initialize_1(self):
pass

@invariant()
def invariant_1(self):
raise ValueError()

@rule()
def rule_1(self):
pass

with capture_out() as o:
with pytest.raises(ValueError):
run_state_machine_as_test(BadInvariant)

result = o.getvalue()
assert (
result
== """\
Falsifying example:
state = BadInvariant()
state.initialize_1()
state.invariant_1()
state.teardown()
"""
)


def test_invariant_present_in_falsifying_example():
@Settings(print_blob=False)
class BadRuleWithGoodInvariants(RuleBasedStateMachine):
def __init__(self):
super().__init__()
self.num = 0

@initialize()
def initialize_1(self):
pass

@invariant(check_during_init=True)
def invariant_1(self):
pass

@invariant(check_during_init=False)
def invariant_2(self):
pass

@precondition(lambda self: self.num > 0)
@invariant()
def invariant_3(self):
pass

@rule()
def rule_1(self):
self.num += 1
if self.num == 2:
raise ValueError()

with capture_out() as o:
with pytest.raises(ValueError):
run_state_machine_as_test(BadRuleWithGoodInvariants)

result = o.getvalue()
assert (
result
== """\
Falsifying example:
state = BadRuleWithGoodInvariants()
state.invariant_1()
state.initialize_1()
state.invariant_1()
state.invariant_2()
state.rule_1()
state.invariant_1()
state.invariant_2()
state.invariant_3()
state.rule_1()
state.teardown()
"""
)


def test_always_runs_at_least_one_step():
class CountSteps(RuleBasedStateMachine):
def __init__(self):
Expand Down

0 comments on commit 5d79bc0

Please sign in to comment.