Debian Codegraph @

hypothesis-python/tests/cover/test_reproduce_failure.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 base64
12 import re
13 import zlib
14
15 import pytest
16
17 from hypothesis import (
18 Verbosity,
19 __version__,
20 example,
21 given,
22 reject,
23 reproduce_failure,
24 settings,
25 strategies as st,
26 )
27 from hypothesis.core import decode_failure, encode_failure
28 from hypothesis.errors import DidNotReproduce, InvalidArgument, UnsatisfiedAssumption
29 from hypothesis.internal.conjecture.choice import choice_equal
30
31 from tests.common.utils import Why, capture_out, no_shrink, xfail_on_crosshair
32 from tests.conjecture.common import nodes, nodes_inline
33
34
35 @example(nodes_inline("0" * 100)) # shorter compressed than not
36 @given(st.lists(nodes()))
37 def test_encoding_loop(nodes):
38 choices = [n.value for n in nodes]
39 looped = decode_failure(encode_failure(choices))
40 assert len(choices) == len(looped)
41 for pre, post in zip(choices, looped, strict=True):
42 assert choice_equal(pre, post)
43
44
45 @example(base64.b64encode(b"\2\3\4"))
46 @example(b"\t")
47 @example(base64.b64encode(b"\1\0")) # zlib error
48 @example(base64.b64encode(b"\1" + zlib.compress(b"\xff"))) # choices_from_bytes error
49 @given(st.binary())
50 def test_decoding_may_fail(t):
51 try:
52 decode_failure(t)
53 reject()
54 except UnsatisfiedAssumption:
55 raise # don't silence the reject()
56 except InvalidArgument:
57 pass
58 except Exception as e:
59 raise AssertionError("Expected an InvalidArgument exception") from e
60
61
62 def test_invalid_base_64_gives_invalid_argument():
63 with pytest.raises(InvalidArgument) as exc_info:
64 decode_failure(b"/")
65 assert "Invalid base64 encoded" in exc_info.value.args[0]
66
67
68 def test_reproduces_the_failure():
69 b = b"hello world"
70 n = len(b)
71
72 @reproduce_failure(__version__, encode_failure([b]))
73 @given(st.binary(min_size=n, max_size=n))
74 def test_outer(x):
75 assert x != b
76
77 @given(st.binary(min_size=n, max_size=n))
78 @reproduce_failure(__version__, encode_failure([b]))
79 def test_inner(x):
80 assert x != b
81
82 with pytest.raises(AssertionError):
83 test_outer()
84 with pytest.raises(AssertionError):
85 test_inner()
86
87
88 def test_errors_if_provided_example_does_not_reproduce_failure():
89 b = b"hello world"
90 n = len(b)
91
92 @reproduce_failure(__version__, encode_failure([b]))
93 @given(st.binary(min_size=n, max_size=n))
94 def test(x):
95 assert x == b
96
97 with pytest.raises(DidNotReproduce):
98 test()
99
100
101 def test_errors_with_did_not_reproduce_if_the_shape_changes():
102 b = b"hello world"
103 n = len(b)
104
105 @reproduce_failure(__version__, encode_failure([b]))
106 @given(st.binary(min_size=n, max_size=n) | st.integers())
107 def test(v):
108 assert v == b
109
110 with pytest.raises(DidNotReproduce):
111 test()
112
113
114 def test_errors_with_did_not_reproduce_if_rejected():
115 b = b"hello world"
116 n = len(b)
117
118 @reproduce_failure(__version__, encode_failure([b]))
119 @given(st.binary(min_size=n, max_size=n))
120 def test(x):
121 reject()
122
123 with pytest.raises(DidNotReproduce):
124 test()
125
126
127 @xfail_on_crosshair(Why.symbolic_outside_context)
128 def test_prints_reproduction_if_requested():
129 failing_example = None
130
131 @settings(print_blob=True, database=None, max_examples=100)
132 @given(st.integers())
133 def test(i):
134 nonlocal failing_example
135 if failing_example is None and i != 0:
136 failing_example = i
137 assert i != failing_example
138
139 with pytest.raises(AssertionError) as err:
140 test()
141 notes = "\n".join(err.value.__notes__)
142 assert "@reproduce_failure" in notes
143
144 exp = re.compile(r"reproduce_failure\(([^)]+)\)", re.MULTILINE)
145 extract = exp.search(notes)
146 reproduction = eval(extract.group(0))
147 test = reproduction(test)
148
149 with pytest.raises(AssertionError):
150 test()
151
152
153 def test_does_not_print_reproduction_for_simple_examples_by_default():
154 @settings(print_blob=False)
155 @given(st.integers())
156 def test(i):
157 raise AssertionError
158
159 with capture_out() as o, pytest.raises(AssertionError):
160 test()
161 assert "@reproduce_failure" not in o.getvalue()
162
163
164 def test_does_not_print_reproduction_for_simple_data_examples_by_default():
165 @settings(print_blob=False)
166 @given(st.data())
167 def test(data):
168 data.draw(st.integers())
169 raise AssertionError
170
171 with capture_out() as o, pytest.raises(AssertionError):
172 test()
173 assert "@reproduce_failure" not in o.getvalue()
174
175
176 def test_does_not_print_reproduction_for_large_data_examples_by_default():
177 @settings(phases=no_shrink, print_blob=False)
178 @given(st.data())
179 def test(data):
180 b = data.draw(st.binary(min_size=1000, max_size=1000))
181 if len(zlib.compress(b)) > 1000:
182 raise ValueError
183
184 with capture_out() as o, pytest.raises(ValueError):
185 test()
186 assert "@reproduce_failure" not in o.getvalue()
187
188
189 class Foo:
190 def __repr__(self):
191 return "not a valid python expression"
192
193
194 def test_does_not_print_reproduction_if_told_not_to():
195 @settings(print_blob=False)
196 @given(st.integers().map(lambda x: Foo()))
197 def test(i):
198 raise ValueError
199
200 with capture_out() as o, pytest.raises(ValueError):
201 test()
202
203 assert "@reproduce_failure" not in o.getvalue()
204
205
206 def test_raises_invalid_if_wrong_version():
207 b = b"hello world"
208 n = len(b)
209
210 @reproduce_failure("1.0.0", encode_failure([b]))
211 @given(st.binary(min_size=n, max_size=n))
212 def test(x):
213 pass
214
215 with pytest.raises(InvalidArgument):
216 test()
217
218
219 def test_does_not_print_reproduction_if_verbosity_set_to_quiet():
220 @given(st.data())
221 @settings(verbosity=Verbosity.quiet, print_blob=False)
222 def test_always_fails(data):
223 assert data.draw(st.just(False))
224
225 with capture_out() as out, pytest.raises(AssertionError):
226 test_always_fails()
227
228 assert "@reproduce_failure" not in out.getvalue()