| 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 functools |
| 12 |
import threading |
| 13 |
from collections import namedtuple |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import ( |
| 18 |
HealthCheck, |
| 19 |
Verbosity, |
| 20 |
assume, |
| 21 |
given, |
| 22 |
note, |
| 23 |
reporting, |
| 24 |
settings, |
| 25 |
strategies as st, |
| 26 |
) |
| 27 |
from hypothesis.errors import Unsatisfiable |
| 28 |
from hypothesis.internal.conjecture.engine import INVALID_THRESHOLD_BASE |
| 29 |
from hypothesis.strategies import ( |
| 30 |
binary, |
| 31 |
booleans, |
| 32 |
builds, |
| 33 |
data, |
| 34 |
floats, |
| 35 |
frozensets, |
| 36 |
integers, |
| 37 |
just, |
| 38 |
lists, |
| 39 |
one_of, |
| 40 |
sampled_from, |
| 41 |
sets, |
| 42 |
text, |
| 43 |
) |
| 44 |
|
| 45 |
from tests.common.utils import ( |
| 46 |
Why, |
| 47 |
assert_falsifying_output, |
| 48 |
capture_out, |
| 49 |
fails, |
| 50 |
fails_with, |
| 51 |
no_shrink, |
| 52 |
skipif_emscripten, |
| 53 |
xfail_on_crosshair, |
| 54 |
) |
| 55 |
from tests.conjecture.common import buffer_size_limit |
| 56 |
|
| 57 |
|
| 58 |
@given(integers(), integers()) |
| 59 |
def test_int_addition_is_commutative(x, y): |
| 60 |
assert x + y == y + x |
| 61 |
|
| 62 |
|
| 63 |
@fails |
| 64 |
@given(text(min_size=1), text(min_size=1)) |
| 65 |
def test_str_addition_is_commutative(x, y): |
| 66 |
assert x + y == y + x |
| 67 |
|
| 68 |
|
| 69 |
@fails |
| 70 |
@given(binary(min_size=1), binary(min_size=1)) |
| 71 |
def test_bytes_addition_is_commutative(x, y): |
| 72 |
# We enforce min_size=1 here to avoid a rare flakiness, where the |
| 73 |
# test passes if x and/or y are b"" for every generated example. |
| 74 |
# (the internal implementation makes this less rare for bytes) |
| 75 |
assert x + y == y + x |
| 76 |
|
| 77 |
|
| 78 |
@given(integers(), integers(), integers()) |
| 79 |
def test_int_addition_is_associative(x, y, z): |
| 80 |
assert x + (y + z) == (x + y) + z |
| 81 |
|
| 82 |
|
| 83 |
@fails |
| 84 |
@given(floats(), floats(), floats()) |
| 85 |
@settings(max_examples=2000) |
| 86 |
def test_float_addition_is_associative(x, y, z): |
| 87 |
assert x + (y + z) == (x + y) + z |
| 88 |
|
| 89 |
|
| 90 |
@given(lists(integers())) |
| 91 |
def test_reversing_preserves_integer_addition(xs): |
| 92 |
assert sum(xs) == sum(reversed(xs)) |
| 93 |
|
| 94 |
|
| 95 |
def test_still_minimizes_on_non_assertion_failures(): |
| 96 |
@settings(max_examples=50) |
| 97 |
@given(integers()) |
| 98 |
def is_not_too_large(x): |
| 99 |
if x >= 10: |
| 100 |
raise ValueError(f"No, {x} is just too large. Sorry") |
| 101 |
|
| 102 |
with pytest.raises(ValueError, match=" 10 "): |
| 103 |
is_not_too_large() |
| 104 |
|
| 105 |
|
| 106 |
@given(integers()) |
| 107 |
def test_integer_division_shrinks_positive_integers(n): |
| 108 |
assume(n > 0) |
| 109 |
assert n / 2 < n |
| 110 |
|
| 111 |
|
| 112 |
class TestCases: |
| 113 |
@given(integers()) |
| 114 |
def test_abs_non_negative(self, x): |
| 115 |
assert abs(x) >= 0 |
| 116 |
assert isinstance(self, TestCases) |
| 117 |
|
| 118 |
@given(x=integers()) |
| 119 |
def test_abs_non_negative_varargs(self, x, *args): |
| 120 |
assert abs(x) >= 0 |
| 121 |
assert isinstance(self, TestCases) |
| 122 |
|
| 123 |
@given(x=integers()) |
| 124 |
def test_abs_non_negative_varargs_kwargs(self, *args, **kw): |
| 125 |
assert abs(kw["x"]) >= 0 |
| 126 |
assert isinstance(self, TestCases) |
| 127 |
|
| 128 |
@given(x=integers()) |
| 129 |
def test_abs_non_negative_varargs_kwargs_only(*args, **kw): |
| 130 |
assert abs(kw["x"]) >= 0 |
| 131 |
assert isinstance(args[0], TestCases) |
| 132 |
|
| 133 |
@fails |
| 134 |
@given(integers()) |
| 135 |
def test_int_is_always_negative(self, x): |
| 136 |
assert x < 0 |
| 137 |
|
| 138 |
@fails |
| 139 |
@given(floats(), floats()) |
| 140 |
def test_float_addition_cancels(self, x, y): |
| 141 |
assert x + (y - x) == y |
| 142 |
|
| 143 |
|
| 144 |
@fails |
| 145 |
@given(x=integers(min_value=0, max_value=3), name=text()) |
| 146 |
def test_can_be_given_keyword_args(x, name): |
| 147 |
assume(x > 0) |
| 148 |
assert len(name) < x |
| 149 |
|
| 150 |
|
| 151 |
@fails |
| 152 |
@given(one_of(floats(), booleans()), one_of(floats(), booleans())) |
| 153 |
def test_one_of_produces_different_values(x, y): |
| 154 |
assert type(x) == type(y) |
| 155 |
|
| 156 |
|
| 157 |
@given(just(42)) |
| 158 |
def test_is_the_answer(x): |
| 159 |
assert x == 42 |
| 160 |
|
| 161 |
|
| 162 |
@given(integers(1, 10)) |
| 163 |
def test_integers_are_in_range(x): |
| 164 |
assert 1 <= x <= 10 |
| 165 |
|
| 166 |
|
| 167 |
@given(integers(min_value=100)) |
| 168 |
def test_integers_from_are_from(x): |
| 169 |
assert x >= 100 |
| 170 |
|
| 171 |
|
| 172 |
def test_does_not_catch_interrupt_during_falsify(): |
| 173 |
called = False |
| 174 |
|
| 175 |
@given(integers()) |
| 176 |
def flaky_base_exception(x): |
| 177 |
nonlocal called |
| 178 |
if not called: |
| 179 |
called = True |
| 180 |
raise KeyboardInterrupt |
| 181 |
|
| 182 |
with pytest.raises(KeyboardInterrupt): |
| 183 |
flaky_base_exception() |
| 184 |
|
| 185 |
|
| 186 |
@given(lists(integers(), unique=True), integers()) |
| 187 |
def test_removing_an_element_from_a_unique_list(xs, y): |
| 188 |
assume(len(set(xs)) == len(xs)) |
| 189 |
|
| 190 |
try: |
| 191 |
xs.remove(y) |
| 192 |
except ValueError: |
| 193 |
pass |
| 194 |
|
| 195 |
assert y not in xs |
| 196 |
|
| 197 |
|
| 198 |
@fails |
| 199 |
@given(lists(integers(), min_size=2), data()) |
| 200 |
def test_removing_an_element_from_a_non_unique_list(xs, data): |
| 201 |
y = data.draw(sampled_from(xs)) |
| 202 |
xs.remove(y) |
| 203 |
assert y not in xs |
| 204 |
|
| 205 |
|
| 206 |
@given(sets(sampled_from(list(range(10))))) |
| 207 |
def test_can_test_sets_sampled_from(xs): |
| 208 |
assert all(isinstance(x, int) for x in xs) |
| 209 |
assert all(0 <= x < 10 for x in xs) |
| 210 |
|
| 211 |
|
| 212 |
mix = one_of(sampled_from([1, 2, 3]), text()) |
| 213 |
|
| 214 |
|
| 215 |
@fails |
| 216 |
@given(mix, mix) |
| 217 |
def test_can_mix_sampling_with_generating(x, y): |
| 218 |
assert type(x) == type(y) |
| 219 |
|
| 220 |
|
| 221 |
@fails |
| 222 |
@given(frozensets(integers())) |
| 223 |
def test_can_find_large_sum_frozenset(xs): |
| 224 |
assert sum(xs) < 100 |
| 225 |
|
| 226 |
|
| 227 |
def test_prints_on_failure_by_default(): |
| 228 |
@given(integers(), integers()) |
| 229 |
@settings(max_examples=1000) |
| 230 |
def test_ints_are_sorted(balthazar, evans): |
| 231 |
assume(evans >= 0) |
| 232 |
assert balthazar <= evans |
| 233 |
|
| 234 |
assert_falsifying_output(test_ints_are_sorted, balthazar=1, evans=0) |
| 235 |
|
| 236 |
|
| 237 |
def test_does_not_print_on_success(): |
| 238 |
@settings(verbosity=Verbosity.normal) |
| 239 |
@given(integers()) |
| 240 |
def test_is_an_int(x): |
| 241 |
return |
| 242 |
|
| 243 |
with capture_out() as out: |
| 244 |
test_is_an_int() |
| 245 |
out = out.getvalue() |
| 246 |
lines = [l.strip() for l in out.split("\n")] |
| 247 |
assert all(not l for l in lines), lines |
| 248 |
|
| 249 |
|
| 250 |
@given(sampled_from([1])) |
| 251 |
def test_can_sample_from_single_element(x): |
| 252 |
assert x == 1 |
| 253 |
|
| 254 |
|
| 255 |
@fails |
| 256 |
@given(lists(integers())) |
| 257 |
def test_list_is_sorted(xs): |
| 258 |
assert sorted(xs) == xs |
| 259 |
|
| 260 |
|
| 261 |
@fails |
| 262 |
@given(floats(1.0, 2.0)) |
| 263 |
def test_is_an_endpoint(x): |
| 264 |
assert x in {1.0, 2.0} |
| 265 |
|
| 266 |
|
| 267 |
def test_breaks_bounds(): |
| 268 |
@fails |
| 269 |
@given(x=integers()) |
| 270 |
@settings(derandomize=True, max_examples=10_000) |
| 271 |
def test_is_bounded(t, x): |
| 272 |
assert x < t |
| 273 |
|
| 274 |
for t in [1, 10, 100, 1000]: |
| 275 |
test_is_bounded(t) |
| 276 |
|
| 277 |
|
| 278 |
@given(x=booleans()) |
| 279 |
def test_can_test_kwargs_only_methods(**kwargs): |
| 280 |
assert isinstance(kwargs["x"], bool) |
| 281 |
|
| 282 |
|
| 283 |
@fails_with(UnicodeEncodeError) |
| 284 |
@given(text()) |
| 285 |
@settings(max_examples=100) |
| 286 |
def test_is_ascii(x): |
| 287 |
x.encode("ascii") |
| 288 |
|
| 289 |
|
| 290 |
@fails |
| 291 |
@given(text()) |
| 292 |
def test_is_not_ascii(x): |
| 293 |
try: |
| 294 |
x.encode("ascii") |
| 295 |
raise AssertionError |
| 296 |
except UnicodeEncodeError: |
| 297 |
pass |
| 298 |
|
| 299 |
|
| 300 |
@fails |
| 301 |
@given(text(min_size=2)) |
| 302 |
@settings(max_examples=100, derandomize=True) |
| 303 |
def test_can_find_string_with_duplicates(s): |
| 304 |
assert len(set(s)) == len(s) |
| 305 |
|
| 306 |
|
| 307 |
@fails |
| 308 |
@given(text(min_size=1)) |
| 309 |
@settings(derandomize=True) |
| 310 |
def test_has_ascii(x): |
| 311 |
if not x: |
| 312 |
return |
| 313 |
ascii_characters = ( |
| 314 |
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \t\n" |
| 315 |
) |
| 316 |
assert any(c in ascii_characters for c in x) |
| 317 |
|
| 318 |
|
| 319 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 320 |
def test_can_derandomize(): |
| 321 |
values = [] |
| 322 |
|
| 323 |
@fails |
| 324 |
@given(integers()) |
| 325 |
@settings(derandomize=True, database=None) |
| 326 |
def test_blah(x): |
| 327 |
values.append(x) |
| 328 |
assert x > 0 |
| 329 |
|
| 330 |
test_blah() |
| 331 |
assert values |
| 332 |
v1 = values |
| 333 |
values = [] |
| 334 |
test_blah() |
| 335 |
assert v1 == values |
| 336 |
|
| 337 |
|
| 338 |
def test_can_run_without_database(): |
| 339 |
@given(integers()) |
| 340 |
@settings(database=None) |
| 341 |
def test_blah(x): |
| 342 |
raise AssertionError |
| 343 |
|
| 344 |
with pytest.raises(AssertionError): |
| 345 |
test_blah() |
| 346 |
|
| 347 |
|
| 348 |
@skipif_emscripten |
| 349 |
def test_can_run_with_database_in_thread(): |
| 350 |
results = [] |
| 351 |
|
| 352 |
@given(integers()) |
| 353 |
def test_blah(x): |
| 354 |
raise ValueError |
| 355 |
|
| 356 |
def run_test(): |
| 357 |
try: |
| 358 |
test_blah() |
| 359 |
except ValueError: |
| 360 |
results.append("success") |
| 361 |
|
| 362 |
# Run once in the main thread and once in another thread. Execution is |
| 363 |
# strictly serial, so no need for locking. |
| 364 |
run_test() |
| 365 |
assert results == ["success"] |
| 366 |
thread = threading.Thread(target=run_test) |
| 367 |
thread.start() |
| 368 |
thread.join() |
| 369 |
assert results == ["success", "success"] |
| 370 |
|
| 371 |
|
| 372 |
@given(integers()) |
| 373 |
def test_can_call_an_argument_f(f): |
| 374 |
# See issue https://github.com/HypothesisWorks/hypothesis-python/issues/38 |
| 375 |
# for details |
| 376 |
pass |
| 377 |
|
| 378 |
|
| 379 |
Litter = namedtuple("Litter", ("kitten1", "kitten2")) |
| 380 |
|
| 381 |
|
| 382 |
@given(builds(Litter, integers(), integers())) |
| 383 |
def test_named_tuples_are_of_right_type(litter): |
| 384 |
assert isinstance(litter, Litter) |
| 385 |
|
| 386 |
|
| 387 |
@fails_with(AttributeError) |
| 388 |
@given(integers().map(lambda x: x.nope)) |
| 389 |
@settings(suppress_health_check=list(HealthCheck)) |
| 390 |
def test_fails_in_reify(x): |
| 391 |
pass |
| 392 |
|
| 393 |
|
| 394 |
@given(text("a")) |
| 395 |
def test_a_text(x): |
| 396 |
assert set(x).issubset(set("a")) |
| 397 |
|
| 398 |
|
| 399 |
@given(text("")) |
| 400 |
def test_empty_text(x): |
| 401 |
assert not x |
| 402 |
|
| 403 |
|
| 404 |
@given(text("abcdefg")) |
| 405 |
def test_mixed_text(x): |
| 406 |
assert set(x).issubset(set("abcdefg")) |
| 407 |
|
| 408 |
|
| 409 |
@xfail_on_crosshair(Why.other, strict=False) # runs ~five failing examples |
| 410 |
def test_when_set_to_no_simplifies_runs_failing_example_twice(): |
| 411 |
failing = [] |
| 412 |
|
| 413 |
@given(integers()) |
| 414 |
@settings( |
| 415 |
phases=no_shrink, |
| 416 |
max_examples=100, |
| 417 |
verbosity=Verbosity.normal, |
| 418 |
report_multiple_bugs=False, |
| 419 |
) |
| 420 |
def foo(x): |
| 421 |
if x > 11: |
| 422 |
note("Lo") |
| 423 |
failing.append(x) |
| 424 |
raise AssertionError |
| 425 |
|
| 426 |
with pytest.raises(AssertionError) as err: |
| 427 |
foo() |
| 428 |
assert len(failing) == 2 |
| 429 |
assert len(set(failing)) == 1 |
| 430 |
assert "Falsifying example" in "\n".join(err.value.__notes__) |
| 431 |
assert "Lo" in err.value.__notes__ |
| 432 |
|
| 433 |
|
| 434 |
@given(integers().filter(lambda x: x % 4 == 0)) |
| 435 |
def test_filtered_values_satisfy_condition(i): |
| 436 |
assert i % 4 == 0 |
| 437 |
|
| 438 |
|
| 439 |
def nameless_const(x): |
| 440 |
def f(u, v): |
| 441 |
return u |
| 442 |
|
| 443 |
return functools.partial(f, x) |
| 444 |
|
| 445 |
|
| 446 |
@given(sets(booleans()).map(nameless_const(2))) |
| 447 |
def test_can_map_nameless(x): |
| 448 |
assert x == 2 |
| 449 |
|
| 450 |
|
| 451 |
@given(integers(0, 10).flatmap(nameless_const(just(3)))) |
| 452 |
def test_can_flatmap_nameless(x): |
| 453 |
assert x == 3 |
| 454 |
|
| 455 |
|
| 456 |
def test_can_be_used_with_none_module(): |
| 457 |
def test_is_cool(i): |
| 458 |
pass |
| 459 |
|
| 460 |
test_is_cool.__module__ = None |
| 461 |
test_is_cool = given(integers())(test_is_cool) |
| 462 |
test_is_cool() |
| 463 |
|
| 464 |
|
| 465 |
def test_does_not_print_notes_if_all_succeed(): |
| 466 |
@given(integers()) |
| 467 |
@settings(verbosity=Verbosity.normal) |
| 468 |
def test(i): |
| 469 |
note("Hi there") |
| 470 |
|
| 471 |
with capture_out() as out, reporting.with_reporter(reporting.default): |
| 472 |
test() |
| 473 |
assert not out.getvalue() |
| 474 |
|
| 475 |
|
| 476 |
def test_prints_notes_once_on_failure(): |
| 477 |
@given(lists(integers())) |
| 478 |
@settings(database=None, verbosity=Verbosity.normal) |
| 479 |
def test(xs): |
| 480 |
note("Hi there") |
| 481 |
if sum(xs) <= 100: |
| 482 |
raise ValueError |
| 483 |
|
| 484 |
with pytest.raises(ValueError) as err: |
| 485 |
test() |
| 486 |
assert err.value.__notes__.count("Hi there") == 1 |
| 487 |
|
| 488 |
|
| 489 |
@given(lists(integers(), max_size=0)) |
| 490 |
def test_empty_lists(xs): |
| 491 |
assert xs == [] |
| 492 |
|
| 493 |
|
| 494 |
@xfail_on_crosshair(Why.other, strict=False) |
| 495 |
def test_given_usable_inline_on_lambdas(): |
| 496 |
xs = [] |
| 497 |
given(booleans())(lambda x: xs.append(x))() |
| 498 |
assert len(xs) == 2 |
| 499 |
assert set(xs) == {False, True} |
| 500 |
|
| 501 |
|
| 502 |
def test_notes_high_filter_rates_in_unsatisfiable_error(): |
| 503 |
@given(st.integers()) |
| 504 |
@settings(suppress_health_check=[HealthCheck.filter_too_much]) |
| 505 |
def f(v): |
| 506 |
assume(False) |
| 507 |
|
| 508 |
with pytest.raises( |
| 509 |
Unsatisfiable, |
| 510 |
match=( |
| 511 |
rf"Unable to satisfy assumptions of f\. {INVALID_THRESHOLD_BASE+1} of " |
| 512 |
rf"{INVALID_THRESHOLD_BASE+1} examples failed a \.filter\(\) or assume\(\)" |
| 513 |
), |
| 514 |
): |
| 515 |
f() |
| 516 |
|
| 517 |
|
| 518 |
# crosshair generates one valid input before verifying the test function, |
| 519 |
# so the Unsatisfiable check never occurs. |
| 520 |
# (not strict due to slowness causing crosshair to bail out on the first input, |
| 521 |
# maybe?) |
| 522 |
@xfail_on_crosshair(Why.other, strict=False) |
| 523 |
def test_notes_high_overrun_rates_in_unsatisfiable_error(): |
| 524 |
@given(st.binary(min_size=100)) |
| 525 |
@settings( |
| 526 |
suppress_health_check=[ |
| 527 |
HealthCheck.data_too_large, |
| 528 |
HealthCheck.too_slow, |
| 529 |
HealthCheck.large_base_example, |
| 530 |
] |
| 531 |
) |
| 532 |
def f(v): |
| 533 |
pass |
| 534 |
|
| 535 |
match = ( |
| 536 |
rf"{INVALID_THRESHOLD_BASE+1} of {INVALID_THRESHOLD_BASE+1} examples were too large to" |
| 537 |
rf" finish generating; try reducing the typical size of your inputs\?" |
| 538 |
) |
| 539 |
with ( |
| 540 |
pytest.raises(Unsatisfiable, match=match), |
| 541 |
buffer_size_limit(10), |
| 542 |
): |
| 543 |
f() |