Debian Codegraph @

hypothesis-python/tests/nocover/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 time
12
13 import pytest
14 from pytest import raises
15
16 from hypothesis import HealthCheck, Phase, given, seed, settings, strategies as st
17 from hypothesis.errors import FailedHealthCheck
18 from hypothesis.internal.conjecture.data import ConjectureData
19 from hypothesis.internal.conjecture.engine import BUFFER_SIZE
20 from hypothesis.strategies._internal.lazy import LazyStrategy
21
22 pytestmark = pytest.mark.skipif(
23 settings.get_current_profile_name() == "crosshair",
24 reason="slow - large number of symbolics",
25 )
26
27 large_strategy = st.binary(min_size=7000, max_size=7000)
28 too_large_strategy = st.tuples(large_strategy, large_strategy)
29
30
31 def test_large_data_will_fail_a_health_check():
32 @given(st.none() | too_large_strategy)
33 @settings(database=None)
34 def test(x):
35 pass
36
37 with raises(FailedHealthCheck) as e:
38 test()
39 assert "maximum allowed entropy" in e.value.args[0]
40
41
42 def test_large_base_example_fails_health_check():
43 @given(large_strategy)
44 def test(b):
45 pass
46
47 with pytest.raises(FailedHealthCheck) as exc:
48 test()
49
50 assert str(HealthCheck.large_base_example) in str(exc.value)
51
52
53 def test_example_that_shrinks_to_overrun_fails_health_check():
54 @given(too_large_strategy | st.none())
55 def test(b):
56 pass
57
58 with pytest.raises(FailedHealthCheck) as exc:
59 test()
60
61 assert str(HealthCheck.large_base_example) in str(exc.value)
62
63
64 slow_down_init = True
65
66
67 def slow_init_integers(*args, **kwargs):
68 # This mimics st.characters() or st.text(), which perform some
69 # expensive Unicode calculations when the cache is empty.
70 global slow_down_init
71 if slow_down_init:
72 time.sleep(0.5) # We monkeypatch time, so this is fast
73 slow_down_init = False
74 return st.integers(*args, **kwargs)
75
76
77 @given(st.data())
78 def test_lazy_slow_initialization_issue_2108_regression(data):
79 # Slow init in strategies wrapped in a LazyStrategy, inside an interactive draw,
80 # should be attributed to drawing from the strategy (not the test function).
81 # Specifically, this used to fail with a DeadlineExceeded error.
82 data.draw(LazyStrategy(slow_init_integers, (), {}))
83
84
85 def test_does_not_trigger_health_check_on_simple_strategies(monkeypatch):
86 existing_draw = ConjectureData.draw_integer
87
88 # We need to make drawing data artificially slow in order to trigger this
89 # effect. This isn't actually slow because time is fake in our CI, but
90 # we need it to pretend to be.
91 def draw_integer(*args, **kwargs):
92 time.sleep(0.001)
93 return existing_draw(*args, **kwargs)
94
95 monkeypatch.setattr(ConjectureData, "draw_integer", draw_integer)
96
97 for _ in range(100):
98 # Setting max_examples=11 ensures we have enough examples for the
99 # health checks to finish running, but cuts the generation short
100 # after that point to allow this test to run in reasonable time.
101 @settings(database=None, max_examples=11, phases=[Phase.generate])
102 @given(st.integers())
103 def test(n):
104 pass
105
106 test()
107
108
109 def test_does_not_trigger_health_check_when_most_examples_are_small():
110 for i in range(10):
111
112 @seed(i)
113 # Setting max_examples=11 ensures we have enough examples for the
114 # health checks to finish running, but cuts the generation short
115 # after that point to allow this test to run in reasonable time.
116 @settings(database=None, max_examples=11, phases=[Phase.generate])
117 @given(
118 st.integers(0, 100).flatmap(
119 lambda n: st.binary(
120 min_size=min(n * 100, BUFFER_SIZE), max_size=n * 100
121 )
122 )
123 )
124 def test(b):
125 pass
126
127 test()