Skip to content

Commit

Permalink
Merge pull request #3802 from Zac-HD/testcase-subclass-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD authored Nov 27, 2023
2 parents e135ded + b9e5a4d commit 4d95d5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
18 changes: 18 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RELEASE_TYPE: patch

This patch supports assigning ``settings = settings(...)`` as a class attribute
on a subclass of a ``.TestCase`` attribute of a :class:`~hypothesis.stateful.RuleBasedStateMachine`.
Previously, this did nothing at all.

.. code-block: python
# works as of this release
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
settings = settings(max_examples=10000)
# the old way still works, but it's more verbose.
MyStateMachine.TestCase.settings = settings(max_examples=10000)
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
pass
Thanks to Joey Tran for reporting these settings-related edge cases in stateful testing.
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class StateMachineTestCase(TestCase):
settings = Settings(deadline=None, suppress_health_check=list(HealthCheck))

def runTest(self):
run_state_machine_as_test(cls)
run_state_machine_as_test(cls, settings=self.settings)

runTest.is_hypothesis_test = True

Expand Down
15 changes: 15 additions & 0 deletions hypothesis-python/tests/nocover/test_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,18 @@ def test_bad_machines_fail(machine):
notes = err.__notes__
steps = [l for l in notes if "Step " in l or "state." in l]
assert 1 <= len(steps) <= 50


class MyStatefulMachine(RuleBasedStateMachine):
def __init__(self):
self.n_steps = 0
super().__init__()

@rule()
def step(self):
self.n_steps += 1
assert self.n_steps <= 10


class TestMyStatefulMachine(MyStatefulMachine.TestCase):
settings = Settings(derandomize=True, stateful_step_count=5)

0 comments on commit 4d95d5a

Please sign in to comment.