Debian Codegraph @

hypothesis-python/tests/cover/test_core.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 unittest
12
13 import pytest
14 from _pytest.outcomes import Failed, Skipped
15
16 from hypothesis import Phase, example, find, given, reject, settings, strategies as st
17 from hypothesis.database import InMemoryExampleDatabase
18 from hypothesis.errors import InvalidArgument, NoSuchExample, Unsatisfiable
19
20
21 def test_stops_after_max_examples_if_satisfying():
22 count = 0
23
24 def track(x):
25 nonlocal count
26 count += 1
27 return False
28
29 max_examples = 100
30
31 with pytest.raises(NoSuchExample):
32 find(st.integers(0, 10000), track, settings=settings(max_examples=max_examples))
33
34 assert count == max_examples
35
36
37 def test_stops_after_ten_times_max_examples_if_not_satisfying():
38 count = 0
39
40 def track(x):
41 nonlocal count
42 count += 1
43 reject()
44
45 max_examples = 100
46 with pytest.raises(Unsatisfiable):
47 find(st.integers(0, 10000), track, settings=settings(max_examples=max_examples))
48
49 # Very occasionally we can generate overflows in generation, which also
50 # count towards our example budget, which means that we don't hit the
51 # maximum.
52 assert count <= 10 * max_examples
53
54
55 some_normal_settings = settings()
56
57
58 def test_is_not_normally_default():
59 assert settings.default is not some_normal_settings
60
61
62 @given(st.booleans())
63 @some_normal_settings
64 def test_settings_are_default_in_given(x):
65 assert settings.default is some_normal_settings
66
67
68 def test_given_shrinks_pytest_helper_errors():
69 final_value = None
70
71 @settings(derandomize=True, max_examples=100)
72 @given(st.integers())
73 def inner(x):
74 nonlocal final_value
75 final_value = x
76 if x > 100:
77 pytest.fail(f"{x=} is too big!")
78
79 with pytest.raises(Failed):
80 inner()
81 assert final_value == 101
82
83
84 def test_pytest_skip_skips_shrinking():
85 seen_large = False
86
87 @settings(derandomize=True, max_examples=100)
88 @given(st.integers())
89 def inner(x):
90 nonlocal seen_large
91 if x > 100:
92 if seen_large:
93 raise Exception("Should never replay a skipped test!")
94 seen_large = True
95 pytest.skip(f"{x=} is too big!")
96
97 with pytest.raises(Skipped):
98 inner()
99
100
101 def test_can_find_with_db_eq_none():
102 find(st.integers(), bool, settings=settings(database=None, max_examples=100))
103
104
105 def test_no_such_example():
106 with pytest.raises(NoSuchExample):
107 find(st.none(), bool, database_key=b"no such example")
108
109
110 def test_validates_strategies_for_test_method():
111 invalid_strategy = st.lists(st.nothing(), min_size=1)
112
113 class TestStrategyValidation:
114 @given(invalid_strategy)
115 def test_method_with_bad_strategy(self, x):
116 pass
117
118 instance = TestStrategyValidation()
119 with pytest.raises(InvalidArgument):
120 instance.test_method_with_bad_strategy()
121
122
123 @example(1)
124 @given(st.integers())
125 @settings(phases=[Phase.target, Phase.shrink, Phase.explain])
126 def no_phases(_):
127 raise Exception
128
129
130 @given(st.integers())
131 @settings(phases=[Phase.explicit])
132 def no_explicit(_):
133 raise Exception
134
135
136 @given(st.integers())
137 @settings(phases=[Phase.reuse], database=InMemoryExampleDatabase())
138 def empty_db(_):
139 raise Exception
140
141
142 @pytest.mark.parametrize(
143 "test_fn",
144 [no_phases, no_explicit, empty_db],
145 ids=lambda t: t.__name__,
146 )
147 def test_non_executed_tests_raise_skipped(test_fn):
148 with pytest.raises(unittest.SkipTest):
149 test_fn()
150
151
152 @pytest.mark.parametrize(
153 "codec, max_codepoint, exclude_categories, categories",
154 [
155 ("ascii", None, None, None),
156 ("ascii", 128, None, None),
157 ("ascii", 100, None, None),
158 ("utf-8", None, None, None),
159 ("utf-8", None, ["Cs"], None),
160 ("utf-8", None, ["N"], None),
161 ("utf-8", None, None, ["N"]),
162 ],
163 )
164 @given(st.data())
165 def test_characters_codec(codec, max_codepoint, exclude_categories, categories, data):
166 strategy = st.characters(
167 codec=codec,
168 max_codepoint=max_codepoint,
169 exclude_categories=exclude_categories,
170 categories=categories,
171 )
172 example = data.draw(strategy)
173 assert example.encode(encoding=codec).decode(encoding=codec) == example