Debian Codegraph @

hypothesis-python/tests/cover/test_health_checks.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import re
12 import time
13
14 import pytest
15
16 from hypothesis import HealthCheck, given, settings, strategies as st
17 from hypothesis.control import assume, current_build_context
18 from hypothesis.errors import FailedHealthCheck, InvalidArgument
19 from hypothesis.internal.compat import int_from_bytes
20 from hypothesis.stateful import (
21 RuleBasedStateMachine,
22 initialize,
23 invariant,
24 rule,
25 run_state_machine_as_test,
26 )
27 from hypothesis.strategies import SearchStrategy
28
29 from tests.common.utils import Why, no_shrink, skipif_time_unpatched, xfail_on_crosshair
30
31 HEALTH_CHECK_SETTINGS = settings(
32 max_examples=11, database=None, suppress_health_check=()
33 )
34
35
36 def test_slow_generation_fails_a_health_check():
37 @settings(HEALTH_CHECK_SETTINGS, deadline=200)
38 @given(st.integers().map(lambda x: time.sleep(0.2)))
39 def test(x):
40 pass
41
42 with pytest.raises(FailedHealthCheck):
43 test()
44
45
46 def test_slow_generation_inline_fails_a_health_check():
47 @settings(HEALTH_CHECK_SETTINGS, deadline=200)
48 @given(st.data())
49 def test(data):
50 data.draw(st.integers().map(lambda x: time.sleep(0.2)))
51
52 with pytest.raises(FailedHealthCheck):
53 test()
54
55
56 def test_default_health_check_can_weaken_specific():
57 import random
58
59 @settings(HEALTH_CHECK_SETTINGS, suppress_health_check=list(HealthCheck))
60 @given(st.lists(st.integers(), min_size=1))
61 def test(x):
62 random.choice(x)
63
64 test()
65
66
67 @pytest.mark.skipif(
68 settings.get_current_profile_name() == "crosshair", reason="nondeterministic"
69 )
70 def test_suppressing_filtering_health_check():
71 forbidden = set()
72
73 def unhealthy_filter(x):
74 if len(forbidden) < 200:
75 forbidden.add(x)
76 return x not in forbidden
77
78 @HEALTH_CHECK_SETTINGS
79 @given(st.integers().filter(unhealthy_filter))
80 def test1(x):
81 raise ValueError
82
83 with pytest.raises(FailedHealthCheck):
84 test1()
85
86 forbidden = set()
87
88 @settings(suppress_health_check=[HealthCheck.filter_too_much, HealthCheck.too_slow])
89 @given(st.integers().filter(unhealthy_filter))
90 def test2(x):
91 raise ValueError
92
93 with pytest.raises(ValueError):
94 test2()
95
96
97 def test_filtering_everything_fails_a_health_check():
98 @given(st.integers().filter(lambda x: False))
99 @settings(database=None, suppress_health_check=())
100 def test(x):
101 pass
102
103 with pytest.raises(FailedHealthCheck, match="filter"):
104 test()
105
106
107 class fails_regularly(SearchStrategy):
108 def do_draw(self, data):
109 b = int_from_bytes(data.draw_bytes(2, 2))
110 assume(b == 3)
111
112
113 def test_filtering_most_things_fails_a_health_check():
114 @given(fails_regularly())
115 @settings(database=None, phases=no_shrink, suppress_health_check=())
116 def test(x):
117 if current_build_context().data.provider.avoid_realization:
118 pytest.skip("symbolic backends can filter efficiently!")
119
120 with pytest.raises(FailedHealthCheck, match="filter"):
121 test()
122
123
124 def test_returning_non_none_is_forbidden():
125 @given(st.integers())
126 def a(x):
127 return 1
128
129 with pytest.raises(FailedHealthCheck):
130 a()
131
132
133 @skipif_time_unpatched # takes forever
134 def test_the_slow_test_health_check_can_be_disabled():
135 @given(st.integers())
136 @settings(deadline=None)
137 def a(x):
138 time.sleep(1000)
139
140 a()
141
142
143 @skipif_time_unpatched # takes forever
144 def test_the_slow_test_health_only_runs_if_health_checks_are_on():
145 @given(st.integers())
146 @settings(suppress_health_check=list(HealthCheck), deadline=None)
147 def a(x):
148 time.sleep(1000)
149
150 a()
151
152
153 class sample_test_runner:
154 @given(st.none())
155 def test(self, _):
156 pass
157
158
159 def test_differing_executors_fails_health_check():
160 sample_test_runner().test()
161 msg = re.escape(str(HealthCheck.differing_executors))
162 with pytest.raises(FailedHealthCheck, match=msg):
163 sample_test_runner().test()
164
165
166 def test_it_is_an_error_to_suppress_non_iterables():
167 with pytest.raises(InvalidArgument):
168 settings(suppress_health_check=1)
169
170
171 def test_it_is_an_error_to_suppress_non_healthchecks():
172 with pytest.raises(InvalidArgument):
173 settings(suppress_health_check=["notahealthcheck"])
174
175
176 class ReturningRuleMachine(RuleBasedStateMachine):
177 @rule()
178 def r(self):
179 return "any non-None value"
180
181
182 class ReturningInitializeMachine(RuleBasedStateMachine):
183 _ = rule()(lambda self: None)
184
185 @initialize()
186 def r(self):
187 return "any non-None value"
188
189
190 class ReturningInvariantMachine(RuleBasedStateMachine):
191 _ = rule()(lambda self: None)
192
193 @invariant(check_during_init=True)
194 def r(self):
195 return "any non-None value"
196
197
198 @pytest.mark.parametrize(
199 "cls", [ReturningRuleMachine, ReturningInitializeMachine, ReturningInvariantMachine]
200 )
201 def test_stateful_returnvalue_healthcheck(cls):
202 with pytest.raises(FailedHealthCheck):
203 run_state_machine_as_test(cls, settings=settings())
204
205
206 def test_nested_given_raises_healthcheck():
207 @given(st.integers())
208 def f(n1):
209 @given(st.integers())
210 def g(n2):
211 pass
212
213 g()
214
215 with pytest.raises(FailedHealthCheck):
216 f()
217
218
219 def test_triply_nested_given_raises_healthcheck():
220 @given(st.integers())
221 @settings(max_examples=10)
222 def f(n1):
223
224 @given(st.integers())
225 @settings(max_examples=10)
226 def g(n2):
227
228 @given(st.integers())
229 @settings(max_examples=10)
230 def h(n3):
231 pass
232
233 h()
234
235 g()
236
237 with pytest.raises(FailedHealthCheck):
238 f()
239
240
241 @xfail_on_crosshair(Why.nested_given)
242 def test_can_suppress_nested_given():
243 @given(st.integers())
244 @settings(suppress_health_check=[HealthCheck.nested_given], max_examples=5)
245 def f(n1):
246
247 @given(st.integers())
248 @settings(max_examples=5)
249 def g(n2):
250 pass
251
252 g()
253
254 f()
255
256
257 def test_cant_suppress_nested_given_on_inner():
258 # nested_given has to be suppressed at the function right above the nesting.
259 # this isn't a principled design choice, but a limitation of how we access
260 # the current settings.
261 @given(st.integers())
262 @settings(max_examples=5)
263 def f(n1):
264
265 @given(st.integers())
266 @settings(suppress_health_check=[HealthCheck.nested_given], max_examples=5)
267 def g(n2):
268 pass
269
270 g()
271
272 with pytest.raises(FailedHealthCheck):
273 f()
274
275
276 @xfail_on_crosshair(Why.nested_given)
277 def test_suppress_triply_nested_given():
278 # both suppressions are necessary here
279 @given(st.integers())
280 @settings(suppress_health_check=[HealthCheck.nested_given], max_examples=5)
281 def f(n1):
282
283 @given(st.integers())
284 @settings(suppress_health_check=[HealthCheck.nested_given], max_examples=5)
285 def g(n2):
286
287 @given(st.integers())
288 @settings(max_examples=5)
289 def h(n3):
290 pass
291
292 h()
293
294 g()
295
296 f()