| 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 Phase, assume, given, settings, strategies as st, target |
| 14 |
from hypothesis.database import InMemoryExampleDatabase |
| 15 |
from hypothesis.errors import FlakyFailure |
| 16 |
from hypothesis.internal.compat import ExceptionGroup |
| 17 |
from hypothesis.internal.conjecture.engine import MIN_TEST_CALLS |
| 18 |
|
| 19 |
from tests.common.utils import ( |
| 20 |
Why, |
| 21 |
assert_output_contains_failure, |
| 22 |
capture_out, |
| 23 |
non_covering_examples, |
| 24 |
xfail_on_crosshair, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def capture_reports(test): |
| 29 |
with capture_out() as o, pytest.raises(ExceptionGroup) as err: |
| 30 |
test() |
| 31 |
|
| 32 |
return o.getvalue() + "\n\n".join( |
| 33 |
f"{e!r}\n" + "\n".join(getattr(e, "__notes__", [])) |
| 34 |
for e in (err.value, *err.value.exceptions) |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 39 |
def test_raises_multiple_failures_with_varying_type(): |
| 40 |
target = None |
| 41 |
|
| 42 |
@settings(database=None, max_examples=100, report_multiple_bugs=True) |
| 43 |
@given(st.integers()) |
| 44 |
def test(i): |
| 45 |
nonlocal target |
| 46 |
if abs(i) < 1000: |
| 47 |
return |
| 48 |
if target is None: |
| 49 |
# Ensure that we have some space to shrink into, so we can't |
| 50 |
# trigger an minimal example and mask the other exception type. |
| 51 |
assume(1003 < abs(i)) |
| 52 |
target = i |
| 53 |
exc_class = TypeError if target == i else ValueError |
| 54 |
raise exc_class |
| 55 |
|
| 56 |
output = capture_reports(test) |
| 57 |
assert "TypeError" in output |
| 58 |
assert "ValueError" in output |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.skipif( |
| 62 |
settings().backend != "hypothesis", reason="no multiple failures on backends (yet?)" |
| 63 |
) |
| 64 |
def test_shows_target_scores_with_multiple_failures(): |
| 65 |
@settings(derandomize=True, max_examples=10_000) |
| 66 |
@given(st.integers()) |
| 67 |
def test(i): |
| 68 |
target(i) |
| 69 |
assert i > 0 |
| 70 |
assert i < 0 |
| 71 |
|
| 72 |
assert "Highest target score:" in capture_reports(test) |
| 73 |
|
| 74 |
|
| 75 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 76 |
def test_raises_multiple_failures_when_position_varies(): |
| 77 |
target = None |
| 78 |
|
| 79 |
@settings(max_examples=100, report_multiple_bugs=True) |
| 80 |
@given(st.integers()) |
| 81 |
def test(i): |
| 82 |
nonlocal target |
| 83 |
if abs(i) < 1000: |
| 84 |
return |
| 85 |
if target is None: |
| 86 |
target = i |
| 87 |
if target == i: |
| 88 |
raise ValueError("loc 1") |
| 89 |
else: |
| 90 |
raise ValueError("loc 2") |
| 91 |
|
| 92 |
output = capture_reports(test) |
| 93 |
assert "loc 1" in output |
| 94 |
assert "loc 2" in output |
| 95 |
|
| 96 |
|
| 97 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 98 |
def test_replays_both_failing_values(): |
| 99 |
target = None |
| 100 |
|
| 101 |
@settings( |
| 102 |
database=InMemoryExampleDatabase(), max_examples=500, report_multiple_bugs=True |
| 103 |
) |
| 104 |
@given(st.integers()) |
| 105 |
def test(i): |
| 106 |
nonlocal target |
| 107 |
if abs(i) < 1000: |
| 108 |
return |
| 109 |
if target is None: |
| 110 |
target = i |
| 111 |
exc_class = TypeError if target == i else ValueError |
| 112 |
raise exc_class |
| 113 |
|
| 114 |
with pytest.raises(ExceptionGroup): |
| 115 |
test() |
| 116 |
|
| 117 |
with pytest.raises(ExceptionGroup): |
| 118 |
test() |
| 119 |
|
| 120 |
|
| 121 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 122 |
@pytest.mark.parametrize("fix", [TypeError, ValueError]) |
| 123 |
def test_replays_slipped_examples_once_initial_bug_is_fixed(fix): |
| 124 |
target = [] |
| 125 |
bug_fixed = False |
| 126 |
|
| 127 |
@settings( |
| 128 |
database=InMemoryExampleDatabase(), max_examples=500, report_multiple_bugs=True |
| 129 |
) |
| 130 |
@given(st.integers()) |
| 131 |
def test(i): |
| 132 |
if abs(i) < 1000: |
| 133 |
return |
| 134 |
if not target: |
| 135 |
target.append(i) |
| 136 |
if i == target[0]: |
| 137 |
if bug_fixed and fix == TypeError: |
| 138 |
return |
| 139 |
raise TypeError |
| 140 |
if len(target) == 1: |
| 141 |
target.append(i) |
| 142 |
if bug_fixed and fix == ValueError: |
| 143 |
return |
| 144 |
if i == target[1]: |
| 145 |
raise ValueError |
| 146 |
|
| 147 |
with pytest.raises(ExceptionGroup): |
| 148 |
test() |
| 149 |
|
| 150 |
bug_fixed = True |
| 151 |
|
| 152 |
with pytest.raises(ValueError if fix == TypeError else TypeError): |
| 153 |
test() |
| 154 |
|
| 155 |
|
| 156 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 157 |
def test_garbage_collects_the_secondary_key(): |
| 158 |
target = [] |
| 159 |
bug_fixed = False |
| 160 |
|
| 161 |
db = InMemoryExampleDatabase() |
| 162 |
|
| 163 |
@settings(database=db, max_examples=500, report_multiple_bugs=True) |
| 164 |
@given(st.integers()) |
| 165 |
def test(i): |
| 166 |
if bug_fixed: |
| 167 |
return |
| 168 |
if abs(i) < 1000: |
| 169 |
return |
| 170 |
if not target: |
| 171 |
target.append(i) |
| 172 |
if i == target[0]: |
| 173 |
raise TypeError |
| 174 |
if len(target) == 1: |
| 175 |
target.append(i) |
| 176 |
if i == target[1]: |
| 177 |
raise ValueError |
| 178 |
|
| 179 |
with pytest.raises(ExceptionGroup): |
| 180 |
test() |
| 181 |
|
| 182 |
bug_fixed = True |
| 183 |
|
| 184 |
def count(): |
| 185 |
return len(non_covering_examples(db)) |
| 186 |
|
| 187 |
prev = count() |
| 188 |
while prev > 0: |
| 189 |
test() |
| 190 |
current = count() |
| 191 |
assert current < prev |
| 192 |
prev = current |
| 193 |
|
| 194 |
|
| 195 |
def test_shrinks_both_failures(): |
| 196 |
first_has_failed = False |
| 197 |
duds = set() |
| 198 |
second_target = None |
| 199 |
|
| 200 |
@settings(database=None, max_examples=1000, report_multiple_bugs=True) |
| 201 |
@given(st.integers(min_value=0)) |
| 202 |
def test(i): |
| 203 |
nonlocal first_has_failed, duds, second_target |
| 204 |
|
| 205 |
if i >= 10000: |
| 206 |
first_has_failed = True |
| 207 |
raise AssertionError |
| 208 |
|
| 209 |
assert i < 10000 |
| 210 |
if first_has_failed: |
| 211 |
if second_target is None: |
| 212 |
for j in range(10000): |
| 213 |
if j not in duds: |
| 214 |
second_target = j |
| 215 |
break |
| 216 |
# to avoid flaky errors, don't error on an input that we previously |
| 217 |
# passed. |
| 218 |
if i not in duds: |
| 219 |
assert i < second_target |
| 220 |
else: |
| 221 |
duds.add(i) |
| 222 |
|
| 223 |
output = capture_reports(test) |
| 224 |
assert_output_contains_failure(output, test, i=10000) |
| 225 |
assert_output_contains_failure(output, test, i=second_target) |
| 226 |
|
| 227 |
|
| 228 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 229 |
def test_handles_flaky_tests_where_only_one_is_flaky(): |
| 230 |
flaky_fixed = False |
| 231 |
|
| 232 |
target = [] |
| 233 |
flaky_failed_once = False |
| 234 |
|
| 235 |
@settings( |
| 236 |
database=InMemoryExampleDatabase(), max_examples=1000, report_multiple_bugs=True |
| 237 |
) |
| 238 |
@given(st.integers()) |
| 239 |
def test(i): |
| 240 |
nonlocal flaky_failed_once |
| 241 |
if abs(i) < 1000: |
| 242 |
return |
| 243 |
if not target: |
| 244 |
target.append(i) |
| 245 |
if i == target[0]: |
| 246 |
raise TypeError |
| 247 |
if flaky_failed_once and not flaky_fixed: |
| 248 |
return |
| 249 |
if len(target) == 1: |
| 250 |
target.append(i) |
| 251 |
if i == target[1]: |
| 252 |
flaky_failed_once = True |
| 253 |
raise ValueError |
| 254 |
|
| 255 |
with pytest.raises(ExceptionGroup) as err: |
| 256 |
test() |
| 257 |
assert any(isinstance(e, FlakyFailure) for e in err.value.exceptions) |
| 258 |
|
| 259 |
flaky_fixed = True |
| 260 |
|
| 261 |
with pytest.raises(ExceptionGroup) as err: |
| 262 |
test() |
| 263 |
assert not any(isinstance(e, FlakyFailure) for e in err.value.exceptions) |
| 264 |
|
| 265 |
|
| 266 |
@pytest.mark.skipif( |
| 267 |
settings().backend != "hypothesis", reason="no multiple failures on backends (yet?)" |
| 268 |
) |
| 269 |
@pytest.mark.parametrize("allow_multi", [True, False]) |
| 270 |
def test_can_disable_multiple_error_reporting(allow_multi): |
| 271 |
seen = set() |
| 272 |
|
| 273 |
@settings(database=None, derandomize=True, report_multiple_bugs=allow_multi) |
| 274 |
@given(st.integers(min_value=0)) |
| 275 |
def test(i): |
| 276 |
# We will pass on the minimal i=0, then fail with a large i, and eventually |
| 277 |
# slip to i=1 and a different error. We check both seen and raised errors. |
| 278 |
if i == 1: |
| 279 |
seen.add(TypeError) |
| 280 |
raise TypeError |
| 281 |
elif i >= 2: |
| 282 |
seen.add(ValueError) |
| 283 |
raise ValueError |
| 284 |
|
| 285 |
with pytest.raises(ExceptionGroup if allow_multi else TypeError): |
| 286 |
test() |
| 287 |
assert seen == {TypeError, ValueError} |
| 288 |
|
| 289 |
|
| 290 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 291 |
def test_finds_multiple_failures_in_generation(): |
| 292 |
special = None |
| 293 |
seen = set() |
| 294 |
|
| 295 |
@settings( |
| 296 |
phases=[Phase.generate, Phase.shrink], |
| 297 |
max_examples=100, |
| 298 |
report_multiple_bugs=True, |
| 299 |
) |
| 300 |
@given(st.integers(min_value=0)) |
| 301 |
def test(x): |
| 302 |
"""Constructs a test so the 10th largeish example we've seen is a |
| 303 |
special failure, and anything new we see after that point that |
| 304 |
is larger than it is a different failure. This demonstrates that we |
| 305 |
can keep generating larger examples and still find new bugs after that |
| 306 |
point.""" |
| 307 |
nonlocal special |
| 308 |
if not special: |
| 309 |
# don't mark duplicate inputs as special and thus erroring, to avoid |
| 310 |
# flakiness where we passed the input the first time but failed it the |
| 311 |
# second. |
| 312 |
if len(seen) >= 10 and x <= 1000 and x not in seen: |
| 313 |
special = x |
| 314 |
else: |
| 315 |
seen.add(x) |
| 316 |
|
| 317 |
if special: |
| 318 |
assert x in seen or x <= special |
| 319 |
assert x != special |
| 320 |
|
| 321 |
with pytest.raises(ExceptionGroup): |
| 322 |
test() |
| 323 |
|
| 324 |
|
| 325 |
def test_stops_immediately_if_not_report_multiple_bugs(): |
| 326 |
seen = set() |
| 327 |
|
| 328 |
@settings(phases=[Phase.generate], report_multiple_bugs=False) |
| 329 |
@given(st.integers()) |
| 330 |
def test(x): |
| 331 |
seen.add(x) |
| 332 |
raise AssertionError |
| 333 |
|
| 334 |
with pytest.raises(AssertionError): |
| 335 |
test() |
| 336 |
assert len(seen) == 1 |
| 337 |
|
| 338 |
|
| 339 |
@pytest.mark.skipif( |
| 340 |
settings().backend != "hypothesis", reason="unclear backend semantics" |
| 341 |
) |
| 342 |
def test_stops_immediately_on_replay(): |
| 343 |
seen = set() |
| 344 |
|
| 345 |
@settings(database=InMemoryExampleDatabase(), phases=tuple(Phase)[:-1]) |
| 346 |
@given(st.integers()) |
| 347 |
def test(x): |
| 348 |
seen.add(x) |
| 349 |
assert x |
| 350 |
|
| 351 |
# On the first run, we look for up to ten examples: |
| 352 |
with pytest.raises(AssertionError): |
| 353 |
test() |
| 354 |
assert 1 < len(seen) <= MIN_TEST_CALLS |
| 355 |
|
| 356 |
# With failing examples in the database, we stop at one. |
| 357 |
seen.clear() |
| 358 |
with pytest.raises(AssertionError): |
| 359 |
test() |
| 360 |
assert len(seen) == 1 |