| 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 time |
| 12 |
from unittest import TestCase |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import ( |
| 17 |
Phase, |
| 18 |
Verbosity, |
| 19 |
assume, |
| 20 |
example, |
| 21 |
given, |
| 22 |
note, |
| 23 |
reporting, |
| 24 |
settings, |
| 25 |
) |
| 26 |
from hypothesis.errors import DeadlineExceeded, HypothesisWarning, InvalidArgument |
| 27 |
from hypothesis.internal.compat import ExceptionGroup |
| 28 |
from hypothesis.strategies import floats, integers, text |
| 29 |
|
| 30 |
from tests.common.utils import ( |
| 31 |
assert_falsifying_output, |
| 32 |
capture_out, |
| 33 |
fails_with, |
| 34 |
skipif_threading, |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
class TestInstanceMethods(TestCase): |
| 39 |
@given(integers()) |
| 40 |
@example(1) |
| 41 |
def test_hi_1(self, x): |
| 42 |
assert isinstance(x, int) |
| 43 |
|
| 44 |
@given(integers()) |
| 45 |
@example(x=1) |
| 46 |
def test_hi_2(self, x): |
| 47 |
assert isinstance(x, int) |
| 48 |
|
| 49 |
@given(x=integers()) |
| 50 |
@example(x=1) |
| 51 |
def test_hi_3(self, x): |
| 52 |
assert isinstance(x, int) |
| 53 |
|
| 54 |
|
| 55 |
def test_kwarg_example_on_testcase(): |
| 56 |
class Stuff(TestCase): |
| 57 |
@given(integers()) |
| 58 |
@example(x=1) |
| 59 |
def test_hi(self, x): |
| 60 |
assert isinstance(x, int) |
| 61 |
|
| 62 |
Stuff("test_hi").test_hi() |
| 63 |
|
| 64 |
|
| 65 |
def test_errors_when_run_with_not_enough_args(): |
| 66 |
@given(integers(), int) |
| 67 |
@example(1) |
| 68 |
def foo(x, y): |
| 69 |
pass |
| 70 |
|
| 71 |
with pytest.raises(TypeError): |
| 72 |
foo() |
| 73 |
|
| 74 |
|
| 75 |
def test_errors_when_run_with_not_enough_kwargs(): |
| 76 |
@given(integers(), int) |
| 77 |
@example(x=1) |
| 78 |
def foo(x, y): |
| 79 |
pass |
| 80 |
|
| 81 |
with pytest.raises(TypeError): |
| 82 |
foo() |
| 83 |
|
| 84 |
|
| 85 |
def test_can_use_examples_after_given(): |
| 86 |
long_str = "This is a very long string that you've no chance of hitting" |
| 87 |
|
| 88 |
@example(long_str) |
| 89 |
@given(text()) |
| 90 |
def test_not_long_str(x): |
| 91 |
assert x != long_str |
| 92 |
|
| 93 |
with pytest.raises(AssertionError): |
| 94 |
test_not_long_str() |
| 95 |
|
| 96 |
|
| 97 |
def test_can_use_examples_before_given(): |
| 98 |
long_str = "This is a very long string that you've no chance of hitting" |
| 99 |
|
| 100 |
@given(text()) |
| 101 |
@example(long_str) |
| 102 |
def test_not_long_str(x): |
| 103 |
assert x != long_str |
| 104 |
|
| 105 |
with pytest.raises(AssertionError): |
| 106 |
test_not_long_str() |
| 107 |
|
| 108 |
|
| 109 |
def test_can_use_examples_around_given(): |
| 110 |
long_str = "This is a very long string that you've no chance of hitting" |
| 111 |
short_str = "Still no chance" |
| 112 |
|
| 113 |
seen = [] |
| 114 |
|
| 115 |
@example(short_str) |
| 116 |
@given(text()) |
| 117 |
@example(long_str) |
| 118 |
def test_not_long_str(x): |
| 119 |
seen.append(x) |
| 120 |
|
| 121 |
test_not_long_str() |
| 122 |
assert set(seen[:2]) == {long_str, short_str} |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize(("x", "y"), [(1, False), (2, True)]) |
| 126 |
@example(z=10) |
| 127 |
@given(z=integers()) |
| 128 |
def test_is_a_thing(x, y, z): |
| 129 |
pass |
| 130 |
|
| 131 |
|
| 132 |
def test_no_args_and_kwargs(): |
| 133 |
with pytest.raises(InvalidArgument): |
| 134 |
example(1, y=2) |
| 135 |
|
| 136 |
|
| 137 |
def test_no_empty_examples(): |
| 138 |
with pytest.raises(InvalidArgument): |
| 139 |
example() |
| 140 |
|
| 141 |
|
| 142 |
def test_does_not_print_on_explicit_examples_if_no_failure(): |
| 143 |
@example(1) |
| 144 |
@given(integers()) |
| 145 |
def test_positive(x): |
| 146 |
assert x > 0 |
| 147 |
|
| 148 |
with ( |
| 149 |
reporting.with_reporter(reporting.default), |
| 150 |
pytest.raises(AssertionError), |
| 151 |
capture_out() as out, |
| 152 |
): |
| 153 |
test_positive() |
| 154 |
out = out.getvalue() |
| 155 |
assert "Falsifying example: test_positive(1)" not in out |
| 156 |
|
| 157 |
|
| 158 |
def test_prints_output_for_explicit_examples(): |
| 159 |
@example(-1) |
| 160 |
@given(integers()) |
| 161 |
def test_positive(x): |
| 162 |
assert x > 0 |
| 163 |
|
| 164 |
assert_falsifying_output(test_positive, "Falsifying explicit", x=-1) |
| 165 |
|
| 166 |
|
| 167 |
def test_prints_verbose_output_for_explicit_examples(): |
| 168 |
@settings(verbosity=Verbosity.verbose) |
| 169 |
@example("NOT AN INTEGER") |
| 170 |
@given(integers()) |
| 171 |
def test_always_passes(x): |
| 172 |
pass |
| 173 |
|
| 174 |
assert_falsifying_output( |
| 175 |
test_always_passes, |
| 176 |
expected_exception=None, |
| 177 |
example_type="Trying explicit", |
| 178 |
x="NOT AN INTEGER", |
| 179 |
) |
| 180 |
|
| 181 |
|
| 182 |
def test_captures_original_repr_of_example(): |
| 183 |
@example(x=[]) |
| 184 |
@given(integers()) |
| 185 |
def test_mutation(x): |
| 186 |
x.append(1) |
| 187 |
assert not x |
| 188 |
|
| 189 |
assert_falsifying_output(test_mutation, "Falsifying explicit", x=[]) |
| 190 |
|
| 191 |
|
| 192 |
def test_examples_are_tried_in_order(): |
| 193 |
@example(x=1) |
| 194 |
@example(x=2) |
| 195 |
@given(integers()) |
| 196 |
@settings(phases=[Phase.explicit]) |
| 197 |
@example(x=3) |
| 198 |
def test(x): |
| 199 |
print(f"x -> {x}") |
| 200 |
|
| 201 |
with capture_out() as out, reporting.with_reporter(reporting.default): |
| 202 |
test() |
| 203 |
ls = out.getvalue().splitlines() |
| 204 |
assert ls == ["x -> 1", "x -> 2", "x -> 3"] |
| 205 |
|
| 206 |
|
| 207 |
def test_prints_note_in_failing_example(): |
| 208 |
@example(x=42) |
| 209 |
@example(x=43) |
| 210 |
@given(integers()) |
| 211 |
def test(x): |
| 212 |
note(f"x -> {x}") |
| 213 |
assert x == 42 |
| 214 |
|
| 215 |
with pytest.raises(AssertionError) as err: |
| 216 |
test() |
| 217 |
assert "x -> 43" in err.value.__notes__ |
| 218 |
assert "x -> 42" not in err.value.__notes__ |
| 219 |
|
| 220 |
|
| 221 |
def test_must_agree_with_number_of_arguments(): |
| 222 |
@example(1, 2) |
| 223 |
@given(integers()) |
| 224 |
def test(a): |
| 225 |
pass |
| 226 |
|
| 227 |
with pytest.raises(InvalidArgument): |
| 228 |
test() |
| 229 |
|
| 230 |
|
| 231 |
@skipif_threading # deadline disabled under threading |
| 232 |
@fails_with(DeadlineExceeded) |
| 233 |
@example(10) |
| 234 |
@settings(phases=[Phase.explicit], deadline=1) |
| 235 |
@given(integers()) |
| 236 |
def test(x): |
| 237 |
time.sleep(10) |
| 238 |
|
| 239 |
|
| 240 |
@given(value=floats(0, 1)) |
| 241 |
@example(value=0.56789) |
| 242 |
@pytest.mark.parametrize("threshold", [0.5, 1]) |
| 243 |
def test_unsatisfied_assumption_during_explicit_example(threshold, value): |
| 244 |
# Regression test, expected to pass / skip depending on parametrize. |
| 245 |
# See https://github.com/HypothesisWorks/hypothesis/issues/2125 |
| 246 |
assume(value < threshold) |
| 247 |
|
| 248 |
|
| 249 |
@pytest.mark.parametrize("exc", [ExceptionGroup, AssertionError]) |
| 250 |
def test_multiple_example_reporting(exc): |
| 251 |
@example(1) |
| 252 |
@example(2) |
| 253 |
@settings(report_multiple_bugs=exc is ExceptionGroup, phases=[Phase.explicit]) |
| 254 |
@given(integers()) |
| 255 |
def inner_test_multiple_failing_examples(x): |
| 256 |
assert x < 2 |
| 257 |
assert x < 1 |
| 258 |
|
| 259 |
with pytest.raises(exc): |
| 260 |
inner_test_multiple_failing_examples() |
| 261 |
|
| 262 |
|
| 263 |
def test_simplifies_multiple_examples_with_same_error(): |
| 264 |
# When multiple examples fail with the same error, only the simplest is shown |
| 265 |
@example(x=1000) |
| 266 |
@example(x=1) |
| 267 |
@example(x=100) |
| 268 |
@settings(report_multiple_bugs=True, phases=[Phase.explicit]) |
| 269 |
@given(x=integers()) |
| 270 |
def test_fn(x): |
| 271 |
assert x < 0 |
| 272 |
|
| 273 |
with pytest.raises(AssertionError) as exc_info: |
| 274 |
test_fn() |
| 275 |
|
| 276 |
err = exc_info.value |
| 277 |
# Should show note about other examples |
| 278 |
assert any( |
| 279 |
"2 other explicit examples also failed" in note for note in err.__notes__ |
| 280 |
) |
| 281 |
# Should show the simplest example (x=1 is shorter than x=100 or x=1000) |
| 282 |
assert any("x=1," in note for note in err.__notes__) |
| 283 |
|
| 284 |
|
| 285 |
def test_shows_all_examples_at_verbose(): |
| 286 |
# At verbose, all examples with same error should be shown |
| 287 |
@example(x=1000) |
| 288 |
@example(x=1) |
| 289 |
@settings( |
| 290 |
report_multiple_bugs=True, phases=[Phase.explicit], verbosity=Verbosity.verbose |
| 291 |
) |
| 292 |
@given(x=integers()) |
| 293 |
def test_fn(x): |
| 294 |
assert x < 0 |
| 295 |
|
| 296 |
with pytest.raises(ExceptionGroup) as exc_info: |
| 297 |
test_fn() |
| 298 |
|
| 299 |
group = exc_info.value |
| 300 |
# Both examples should be shown |
| 301 |
assert len(group.exceptions) == 2 |
| 302 |
# No simplification note should be present |
| 303 |
for exc in group.exceptions: |
| 304 |
assert not any( |
| 305 |
"other explicit example" in note for note in getattr(exc, "__notes__", []) |
| 306 |
) |
| 307 |
|
| 308 |
|
| 309 |
def test_different_errors_not_simplified(): |
| 310 |
# Examples with different errors should all be shown |
| 311 |
@example(x=1) |
| 312 |
@example(x=2) |
| 313 |
@settings(report_multiple_bugs=True, phases=[Phase.explicit]) |
| 314 |
@given(x=integers()) |
| 315 |
def test_fn(x): |
| 316 |
if x == 1: |
| 317 |
raise ValueError("one") |
| 318 |
raise TypeError("two") |
| 319 |
|
| 320 |
with pytest.raises(ExceptionGroup) as exc_info: |
| 321 |
test_fn() |
| 322 |
|
| 323 |
assert {type(e) for e in exc_info.value.exceptions} == {ValueError, TypeError} |
| 324 |
|
| 325 |
|
| 326 |
@example(text()) |
| 327 |
@given(text()) |
| 328 |
def test_example_decorator_accepts_strategies(s): |
| 329 |
"""The custom error message only happens when the test has already failed.""" |
| 330 |
|
| 331 |
|
| 332 |
def test_helpful_message_when_example_fails_because_it_was_passed_a_strategy(): |
| 333 |
@example(text()) |
| 334 |
@given(text()) |
| 335 |
def t(s): |
| 336 |
assert isinstance(s, str) |
| 337 |
|
| 338 |
try: |
| 339 |
t() |
| 340 |
except HypothesisWarning as err: |
| 341 |
assert isinstance(err.__cause__, AssertionError) |
| 342 |
else: |
| 343 |
raise NotImplementedError("should be unreachable") |
| 344 |
|
| 345 |
|
| 346 |
def test_stop_silently_dropping_examples_when_decorator_is_applied_to_itself(): |
| 347 |
def f(): |
| 348 |
pass |
| 349 |
|
| 350 |
test = example("outer")(example("inner"))(f) |
| 351 |
assert len(test.hypothesis_explicit_examples) == 2 |