| 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 |
from dataclasses import dataclass |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import Verbosity, assume, given, reject, reporting, settings |
| 16 |
from hypothesis.control import ( |
| 17 |
BuildContext, |
| 18 |
_current_build_context, |
| 19 |
_event_to_string, |
| 20 |
cleanup, |
| 21 |
current_build_context, |
| 22 |
currently_in_test_context, |
| 23 |
event, |
| 24 |
note, |
| 25 |
) |
| 26 |
from hypothesis.errors import ( |
| 27 |
HypothesisDeprecationWarning, |
| 28 |
InvalidArgument, |
| 29 |
UnsatisfiedAssumption, |
| 30 |
) |
| 31 |
from hypothesis.internal.compat import ExceptionGroup |
| 32 |
from hypothesis.internal.conjecture.data import ConjectureData |
| 33 |
from hypothesis.stateful import RuleBasedStateMachine, rule |
| 34 |
from hypothesis.strategies import integers |
| 35 |
|
| 36 |
from tests.common.utils import capture_out |
| 37 |
|
| 38 |
|
| 39 |
def bc(): |
| 40 |
return BuildContext(ConjectureData.for_choices([]), wrapped_test=None) |
| 41 |
|
| 42 |
|
| 43 |
def test_cannot_cleanup_with_no_context(): |
| 44 |
with pytest.raises(InvalidArgument): |
| 45 |
cleanup(lambda: None) |
| 46 |
assert _current_build_context.value is None |
| 47 |
|
| 48 |
|
| 49 |
def test_cannot_event_with_no_context(): |
| 50 |
with pytest.raises(InvalidArgument): |
| 51 |
event("hi") |
| 52 |
assert _current_build_context.value is None |
| 53 |
|
| 54 |
|
| 55 |
def test_cleanup_executes_on_leaving_build_context(): |
| 56 |
data = [] |
| 57 |
with bc(): |
| 58 |
cleanup(lambda: data.append(1)) |
| 59 |
assert not data |
| 60 |
assert data == [1] |
| 61 |
assert _current_build_context.value is None |
| 62 |
|
| 63 |
|
| 64 |
def test_can_nest_build_context(): |
| 65 |
data = [] |
| 66 |
with bc(): |
| 67 |
cleanup(lambda: data.append(1)) |
| 68 |
with bc(): |
| 69 |
cleanup(lambda: data.append(2)) |
| 70 |
assert not data |
| 71 |
assert data == [2] |
| 72 |
assert data == [2, 1] |
| 73 |
assert _current_build_context.value is None |
| 74 |
|
| 75 |
|
| 76 |
def test_does_not_suppress_exceptions(): |
| 77 |
with pytest.raises(AssertionError), bc(): |
| 78 |
raise AssertionError |
| 79 |
assert _current_build_context.value is None |
| 80 |
|
| 81 |
|
| 82 |
def test_suppresses_exceptions_in_teardown(): |
| 83 |
with pytest.raises(ValueError) as exc_info, bc(): |
| 84 |
|
| 85 |
def foo(): |
| 86 |
raise ValueError |
| 87 |
|
| 88 |
cleanup(foo) |
| 89 |
raise AssertionError |
| 90 |
|
| 91 |
assert isinstance(exc_info.value, ValueError) |
| 92 |
assert isinstance(exc_info.value.__cause__, AssertionError) |
| 93 |
|
| 94 |
|
| 95 |
def test_runs_multiple_cleanup_with_teardown(): |
| 96 |
with pytest.raises(ExceptionGroup) as exc_info, bc(): |
| 97 |
|
| 98 |
def foo(): |
| 99 |
raise ValueError |
| 100 |
|
| 101 |
def bar(): |
| 102 |
raise TypeError |
| 103 |
|
| 104 |
cleanup(foo) |
| 105 |
cleanup(bar) |
| 106 |
raise AssertionError |
| 107 |
|
| 108 |
assert isinstance(exc_info.value, ExceptionGroup) |
| 109 |
assert isinstance(exc_info.value.__cause__, AssertionError) |
| 110 |
assert {type(e) for e in exc_info.value.exceptions} == {ValueError, TypeError} |
| 111 |
assert _current_build_context.value is None |
| 112 |
|
| 113 |
|
| 114 |
def test_raises_error_if_cleanup_fails_but_block_does_not(): |
| 115 |
with pytest.raises(ValueError), bc(): |
| 116 |
|
| 117 |
def foo(): |
| 118 |
raise ValueError |
| 119 |
|
| 120 |
cleanup(foo) |
| 121 |
assert _current_build_context.value is None |
| 122 |
|
| 123 |
|
| 124 |
def test_raises_if_note_out_of_context(): |
| 125 |
with pytest.raises(InvalidArgument): |
| 126 |
note("Hi") |
| 127 |
|
| 128 |
|
| 129 |
def test_deprecation_warning_if_assume_out_of_context(): |
| 130 |
with pytest.warns( |
| 131 |
HypothesisDeprecationWarning, |
| 132 |
match="Using `assume` outside a property-based test is deprecated", |
| 133 |
): |
| 134 |
assume(True) |
| 135 |
|
| 136 |
|
| 137 |
def test_deprecation_warning_if_reject_out_of_context(): |
| 138 |
with ( |
| 139 |
pytest.warns( |
| 140 |
HypothesisDeprecationWarning, |
| 141 |
match="Using `reject` outside a property-based test is deprecated", |
| 142 |
), |
| 143 |
pytest.raises(UnsatisfiedAssumption), |
| 144 |
): |
| 145 |
reject() |
| 146 |
|
| 147 |
|
| 148 |
def test_raises_if_current_build_context_out_of_context(): |
| 149 |
with pytest.raises(InvalidArgument): |
| 150 |
current_build_context() |
| 151 |
|
| 152 |
|
| 153 |
def test_current_build_context_is_current(): |
| 154 |
with bc() as a: |
| 155 |
assert current_build_context() is a |
| 156 |
|
| 157 |
|
| 158 |
def test_prints_all_notes_in_verbose_mode(): |
| 159 |
# slightly roundabout because @example messes with verbosity - see #1521 |
| 160 |
messages = set() |
| 161 |
|
| 162 |
@settings(verbosity=Verbosity.debug, database=None) |
| 163 |
@given(integers(1, 10)) |
| 164 |
def test(x): |
| 165 |
msg = f"x -> {x}" |
| 166 |
note(msg) |
| 167 |
messages.add(msg) |
| 168 |
assert x < 5 |
| 169 |
|
| 170 |
with ( |
| 171 |
capture_out() as out, |
| 172 |
reporting.with_reporter(reporting.default), |
| 173 |
pytest.raises(AssertionError), |
| 174 |
): |
| 175 |
test() |
| 176 |
v = out.getvalue() |
| 177 |
for x in sorted(messages): |
| 178 |
assert x in v |
| 179 |
|
| 180 |
|
| 181 |
@dataclass |
| 182 |
class CanBePrettyPrinted: |
| 183 |
n: int |
| 184 |
|
| 185 |
|
| 186 |
def test_note_pretty_prints(): |
| 187 |
reports = [] |
| 188 |
|
| 189 |
@given(integers(1, 10)) |
| 190 |
def test(n): |
| 191 |
with reporting.with_reporter(reports.append): |
| 192 |
note(CanBePrettyPrinted(n)) |
| 193 |
assert n != 5 |
| 194 |
|
| 195 |
with pytest.raises(AssertionError): |
| 196 |
test() |
| 197 |
|
| 198 |
assert reports == ["CanBePrettyPrinted(n=5)"] |
| 199 |
|
| 200 |
|
| 201 |
def test_not_currently_in_hypothesis(): |
| 202 |
assert currently_in_test_context() is False |
| 203 |
|
| 204 |
|
| 205 |
@given(integers()) |
| 206 |
def test_currently_in_hypothesis(_): |
| 207 |
assert currently_in_test_context() is True |
| 208 |
|
| 209 |
|
| 210 |
class ContextMachine(RuleBasedStateMachine): |
| 211 |
@rule() |
| 212 |
def step(self): |
| 213 |
assert currently_in_test_context() is True |
| 214 |
|
| 215 |
|
| 216 |
test_currently_in_stateful_test = ContextMachine.TestCase |
| 217 |
|
| 218 |
|
| 219 |
def test_can_convert_non_weakref_types_to_event_strings(): |
| 220 |
_event_to_string((), avoid_realization=True) |
| 221 |
_event_to_string((), avoid_realization=False) |