Debian Codegraph @

hypothesis-python/tests/cover/test_replay_logic.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 pytest
12
13 from hypothesis import given, settings, strategies as st
14 from hypothesis.database import InMemoryExampleDatabase
15 from hypothesis.internal.compat import ExceptionGroup
16
17
18 def test_does_not_shrink_on_replay():
19 database = InMemoryExampleDatabase()
20
21 call_count = 0
22
23 is_first = True
24 last = None
25
26 @settings(
27 database=database,
28 report_multiple_bugs=False,
29 derandomize=False,
30 max_examples=1000,
31 )
32 @given(st.lists(st.integers(), unique=True, min_size=3))
33 def test(ls):
34 nonlocal call_count, is_first, last
35 if is_first and last is not None:
36 assert ls == last
37 is_first = False
38 last = ls
39 call_count += 1
40 raise AssertionError
41
42 with pytest.raises(AssertionError):
43 test()
44
45 assert last is not None
46
47 call_count = 0
48 is_first = True
49
50 with pytest.raises(AssertionError):
51 test()
52
53 assert call_count == 2
54
55
56 def test_does_not_shrink_on_replay_with_multiple_bugs():
57 database = InMemoryExampleDatabase()
58 call_count = 0
59 raised = False
60 marker = 1000093
61
62 @settings(
63 database=database,
64 report_multiple_bugs=True,
65 derandomize=False,
66 max_examples=1000,
67 )
68 @given(st.integers())
69 def test(n):
70 nonlocal call_count, raised
71 call_count += 1
72 if n >= marker:
73 raised = True
74 raise AssertionError
75 elif n < marker and raised:
76 raise AssertionError
77
78 with pytest.raises(ExceptionGroup):
79 test()
80
81 call_count = 0
82
83 with pytest.raises(ExceptionGroup):
84 test()
85
86 assert call_count == 4
87
88
89 def test_will_always_shrink_if_previous_example_does_not_replay():
90 database = InMemoryExampleDatabase()
91
92 good = set()
93 last = None
94
95 @settings(
96 database=database,
97 report_multiple_bugs=True,
98 derandomize=False,
99 max_examples=1000,
100 )
101 @given(st.integers(min_value=0))
102 def test(n):
103 nonlocal last
104 if n not in good:
105 last = n
106 raise AssertionError
107
108 for i in range(20):
109 with pytest.raises(AssertionError):
110 test()
111 assert last == i
112 good.add(last)
113
114
115 def test_will_shrink_if_the_previous_example_does_not_look_right():
116 database = InMemoryExampleDatabase()
117
118 last = None
119
120 first_test = True
121
122 @settings(database=database, report_multiple_bugs=True, derandomize=False)
123 @given(st.data())
124 def test(data):
125 nonlocal last
126 m = data.draw(st.integers())
127 last = m
128 if first_test:
129 data.draw(st.integers())
130 assert m < 10000
131 else:
132 raise AssertionError
133
134 with pytest.raises(AssertionError):
135 test()
136
137 assert last is not None
138 assert last > 0
139
140 first_test = False
141
142 with pytest.raises(AssertionError):
143 test()
144
145 assert last == 0