17
17
import pytest
18
18
19
19
from hypothesis ._settings import is_in_ci
20
+ from hypothesis .errors import NonInteractiveExampleWarning
21
+ from hypothesis .internal .compat import add_note
20
22
from hypothesis .internal .detection import is_hypothesis_test
21
23
22
24
from tests .common import TIME_INCREMENT
@@ -106,20 +108,20 @@ def pytest_runtest_call(item):
106
108
# This hookwrapper checks for PRNG state leaks from Hypothesis tests.
107
109
# See: https://github.com/HypothesisWorks/hypothesis/issues/1919
108
110
if not (hasattr (item , "obj" ) and is_hypothesis_test (item .obj )):
109
- yield
111
+ outcome = yield
110
112
elif "pytest_randomly" in sys .modules :
111
113
# See https://github.com/HypothesisWorks/hypothesis/issues/3041 - this
112
114
# branch exists to make it easier on external contributors, but should
113
115
# never run in our CI (because that would disable the check entirely).
114
116
assert not is_in_ci ()
115
- yield
117
+ outcome = yield
116
118
else :
117
119
# We start by peturbing the state of the PRNG, because repeatedly
118
120
# leaking PRNG state resets state_after to the (previously leaked)
119
121
# state_before, and that just shows as "no use of random".
120
122
random .seed (independent_random .randrange (2 ** 32 ))
121
123
before = random .getstate ()
122
- yield
124
+ outcome = yield
123
125
after = random .getstate ()
124
126
if before != after :
125
127
if after in random_states_after_tests :
@@ -129,3 +131,11 @@ def pytest_runtest_call(item):
129
131
"same global `random.getstate()`; this is probably a nasty bug!"
130
132
)
131
133
random_states_after_tests [after ] = item .nodeid
134
+
135
+ # Annotate usage of .example() with a hint about alternatives
136
+ if isinstance (outcome .exception , NonInteractiveExampleWarning ):
137
+ add_note (
138
+ outcome .exception ,
139
+ "For hypothesis' own test suite, consider using one of the helper "
140
+ "methods in tests.common.debug instead." ,
141
+ )
0 commit comments