From 2becc5d91fe42f6e61a43641c3138296769eeb98 Mon Sep 17 00:00:00 2001 From: Zac-HD Date: Sun, 6 Jan 2019 14:28:42 +1100 Subject: [PATCH] Deterministic scheduling via Hypothesis --- newsfragments/73.feature.rst | 3 +++ pytest_trio/plugin.py | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 newsfragments/73.feature.rst diff --git a/newsfragments/73.feature.rst b/newsfragments/73.feature.rst new file mode 100644 index 0000000..9993cdf --- /dev/null +++ b/newsfragments/73.feature.rst @@ -0,0 +1,3 @@ +pytest-trio now makes the Trio scheduler deterministic while running +inside a Hypothesis test. Hopefully you won't see any change, but if +you had scheduler-dependent bugs Hypothesis will be more effective now. diff --git a/pytest_trio/plugin.py b/pytest_trio/plugin.py index b567cf9..0126814 100644 --- a/pytest_trio/plugin.py +++ b/pytest_trio/plugin.py @@ -22,6 +22,17 @@ # Ordered dict (and **kwargs) not available with Python<3.6 ORDERED_DICTS = False +try: + import hypothesis.strategies as st +except ImportError: # pragma: no cover + pass +else: + # On recent versions of Hypothesis, make the Trio scheduler deterministic + # even though it uses a module-scoped Random instance. This works + # regardless of whether or not the random_module strategy is used. + if hasattr(st.random_module, 'register'): # pragma: no branch + st.random_module.register(trio._core._run._r) + def pytest_addoption(parser): parser.addini( @@ -345,7 +356,8 @@ def pytest_runtest_call(item): item.obj.hypothesis.inner_test = _trio_test_runner_factory( item, item.obj.hypothesis.inner_test ) - elif getattr(item.obj, 'is_hypothesis_test', False): + elif getattr(item.obj, 'is_hypothesis_test', + False): # pragma: no cover pytest.fail( 'test function `%r` is using Hypothesis, but pytest-trio ' 'only works with Hypothesis 3.64.0 or later.' % item