Skip to content

Commit 4d95d5a

Browse files
authored
Merge pull request #3802 from Zac-HD/testcase-subclass-settings
2 parents e135ded + b9e5a4d commit 4d95d5a

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch supports assigning ``settings = settings(...)`` as a class attribute
4+
on a subclass of a ``.TestCase`` attribute of a :class:`~hypothesis.stateful.RuleBasedStateMachine`.
5+
Previously, this did nothing at all.
6+
7+
.. code-block: python
8+
9+
# works as of this release
10+
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
11+
settings = settings(max_examples=10000)
12+
13+
# the old way still works, but it's more verbose.
14+
MyStateMachine.TestCase.settings = settings(max_examples=10000)
15+
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
16+
pass
17+
18+
Thanks to Joey Tran for reporting these settings-related edge cases in stateful testing.

hypothesis-python/src/hypothesis/stateful.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class StateMachineTestCase(TestCase):
402402
settings = Settings(deadline=None, suppress_health_check=list(HealthCheck))
403403

404404
def runTest(self):
405-
run_state_machine_as_test(cls)
405+
run_state_machine_as_test(cls, settings=self.settings)
406406

407407
runTest.is_hypothesis_test = True
408408

hypothesis-python/tests/nocover/test_stateful.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,18 @@ def test_bad_machines_fail(machine):
186186
notes = err.__notes__
187187
steps = [l for l in notes if "Step " in l or "state." in l]
188188
assert 1 <= len(steps) <= 50
189+
190+
191+
class MyStatefulMachine(RuleBasedStateMachine):
192+
def __init__(self):
193+
self.n_steps = 0
194+
super().__init__()
195+
196+
@rule()
197+
def step(self):
198+
self.n_steps += 1
199+
assert self.n_steps <= 10
200+
201+
202+
class TestMyStatefulMachine(MyStatefulMachine.TestCase):
203+
settings = Settings(derandomize=True, stateful_step_count=5)

0 commit comments

Comments
 (0)