| 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 |
| 14 |
from hypothesis.errors import Flaky, FlakyFailure |
| 15 |
from hypothesis.strategies import composite, integers, none |
| 16 |
|
| 17 |
from tests.common.utils import Why, skipif_threading, xfail_on_crosshair |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.parametrize( |
| 21 |
"e", [KeyboardInterrupt, SystemExit, GeneratorExit, ValueError] |
| 22 |
) |
| 23 |
def test_exception_propagates_fine(e): |
| 24 |
@given(integers()) |
| 25 |
def test_raise(x): |
| 26 |
raise e |
| 27 |
|
| 28 |
with pytest.raises(e): |
| 29 |
test_raise() |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
"e", [KeyboardInterrupt, SystemExit, GeneratorExit, ValueError] |
| 34 |
) |
| 35 |
def test_exception_propagates_fine_from_strategy(e): |
| 36 |
@composite |
| 37 |
def interrupt_eventually(draw): |
| 38 |
raise e |
| 39 |
# this line will not be executed, but must be here |
| 40 |
# to pass draw function static reference check |
| 41 |
return draw(none()) |
| 42 |
|
| 43 |
@given(interrupt_eventually()) |
| 44 |
def test_do_nothing(x): |
| 45 |
pass |
| 46 |
|
| 47 |
with pytest.raises(e): |
| 48 |
test_do_nothing() |
| 49 |
|
| 50 |
|
| 51 |
@xfail_on_crosshair(Why.other, strict=False) # extra replay from backend switch |
| 52 |
@pytest.mark.parametrize("e", [KeyboardInterrupt, ValueError]) |
| 53 |
def test_baseexception_no_rerun_no_flaky(e): |
| 54 |
runs = 0 |
| 55 |
interrupt = 3 |
| 56 |
|
| 57 |
@given(integers()) |
| 58 |
def test_raise_baseexception(x): |
| 59 |
nonlocal runs |
| 60 |
runs += 1 |
| 61 |
if runs == interrupt: |
| 62 |
raise e |
| 63 |
|
| 64 |
if issubclass(e, (KeyboardInterrupt, SystemExit, GeneratorExit)): |
| 65 |
# Here SystemExit and GeneratorExit are passed through |
| 66 |
with pytest.raises(e): |
| 67 |
test_raise_baseexception() |
| 68 |
|
| 69 |
assert runs == interrupt |
| 70 |
else: |
| 71 |
with pytest.raises(FlakyFailure): |
| 72 |
test_raise_baseexception() |
| 73 |
|
| 74 |
|
| 75 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) # KI and GE only |
| 76 |
@pytest.mark.parametrize( |
| 77 |
"e", [KeyboardInterrupt, SystemExit, GeneratorExit, ValueError] |
| 78 |
) |
| 79 |
def test_baseexception_in_strategy_no_rerun_no_flaky(e): |
| 80 |
runs = 0 |
| 81 |
interrupt = 3 |
| 82 |
|
| 83 |
@composite |
| 84 |
def interrupt_eventually(draw): |
| 85 |
nonlocal runs |
| 86 |
runs += 1 |
| 87 |
if runs == interrupt: |
| 88 |
raise e |
| 89 |
return draw(integers()) |
| 90 |
|
| 91 |
@given(interrupt_eventually()) |
| 92 |
def test_do_nothing(x): |
| 93 |
pass |
| 94 |
|
| 95 |
if issubclass(e, KeyboardInterrupt): |
| 96 |
with pytest.raises(e): |
| 97 |
test_do_nothing() |
| 98 |
|
| 99 |
assert runs == interrupt |
| 100 |
|
| 101 |
else: |
| 102 |
# Now SystemExit and GeneratorExit are caught like other exceptions |
| 103 |
with pytest.raises(Flaky): |
| 104 |
test_do_nothing() |
| 105 |
|
| 106 |
|
| 107 |
TEMPLATE = """ |
| 108 |
from hypothesis import given, note, strategies as st |
| 109 |
|
| 110 |
@st.composite |
| 111 |
def things(draw): |
| 112 |
raise {exception} |
| 113 |
# this line will not be executed, but must be here |
| 114 |
# to pass draw function static reference check |
| 115 |
return draw(st.none()) |
| 116 |
|
| 117 |
|
| 118 |
@given(st.data(), st.integers()) |
| 119 |
def test(data, x): |
| 120 |
if x > 100: |
| 121 |
data.draw({strategy}) |
| 122 |
raise {exception} |
| 123 |
""" |
| 124 |
|
| 125 |
|
| 126 |
@skipif_threading # something in pytest here is not thread safe |
| 127 |
@pytest.mark.parametrize("exc_name", ["SystemExit", "GeneratorExit"]) |
| 128 |
@pytest.mark.parametrize("use_composite", [True, False]) |
| 129 |
def test_explanations(testdir, exc_name, use_composite): |
| 130 |
code = TEMPLATE.format( |
| 131 |
exception=exc_name, strategy="things()" if use_composite else "st.none()" |
| 132 |
) |
| 133 |
test_file = str(testdir.makepyfile(code)) |
| 134 |
pytest_stdout = str(testdir.runpytest_inprocess(test_file, "--tb=native").stdout) |
| 135 |
assert "x=101" in pytest_stdout |
| 136 |
assert exc_name in pytest_stdout |