Skip to content

Commit 3775f98

Browse files
committed
Deterministic scheduling via Hypothesis
1 parent e828788 commit 3775f98

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

newsfragments/73.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest-trio now makes the Trio scheduler deterministic while running
2+
inside a Hypothesis test. Hopefully you won't see any change, but if
3+
you had scheduler-dependent bugs Hypothesis will be more effective now.

pytest_trio/plugin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
# Ordered dict (and **kwargs) not available with Python<3.6
2323
ORDERED_DICTS = False
2424

25+
try:
26+
from hypothesis import register_random
27+
except ImportError: # pragma: no cover
28+
pass
29+
else:
30+
# On recent versions of Hypothesis, make the Trio scheduler deterministic
31+
# even though it uses a module-scoped Random instance. This works
32+
# regardless of whether or not the random_module strategy is used.
33+
register_random(trio._core._run._r)
34+
2535

2636
def pytest_addoption(parser):
2737
parser.addini(
@@ -345,7 +355,8 @@ def pytest_runtest_call(item):
345355
item.obj.hypothesis.inner_test = _trio_test_runner_factory(
346356
item, item.obj.hypothesis.inner_test
347357
)
348-
elif getattr(item.obj, 'is_hypothesis_test', False):
358+
elif getattr(item.obj, 'is_hypothesis_test',
359+
False): # pragma: no cover
349360
pytest.fail(
350361
'test function `%r` is using Hypothesis, but pytest-trio '
351362
'only works with Hypothesis 3.64.0 or later.' % item

0 commit comments

Comments
 (0)