| 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 contextlib import contextmanager |
| 12 |
|
| 13 |
from hypothesis import example, find, given |
| 14 |
from hypothesis._settings import Verbosity, settings |
| 15 |
from hypothesis.reporting import default as default_reporter, with_reporter |
| 16 |
from hypothesis.strategies import booleans, integers, lists |
| 17 |
|
| 18 |
from tests.common.debug import minimal |
| 19 |
from tests.common.utils import Why, capture_out, fails, xfail_on_crosshair |
| 20 |
|
| 21 |
|
| 22 |
@contextmanager |
| 23 |
def capture_verbosity(): |
| 24 |
with capture_out() as o, with_reporter(default_reporter): |
| 25 |
yield o |
| 26 |
|
| 27 |
|
| 28 |
def test_prints_intermediate_in_success(): |
| 29 |
with capture_verbosity() as o: |
| 30 |
|
| 31 |
@settings(verbosity=Verbosity.verbose) |
| 32 |
@given(booleans()) |
| 33 |
def test_works(x): |
| 34 |
pass |
| 35 |
|
| 36 |
test_works() |
| 37 |
assert "Trying example" in o.getvalue() |
| 38 |
|
| 39 |
|
| 40 |
def test_does_not_log_in_quiet_mode(): |
| 41 |
with capture_verbosity() as o: |
| 42 |
|
| 43 |
@fails |
| 44 |
@settings(verbosity=Verbosity.quiet, print_blob=False) |
| 45 |
@given(integers()) |
| 46 |
def test_foo(x): |
| 47 |
raise AssertionError |
| 48 |
|
| 49 |
test_foo() |
| 50 |
assert not o.getvalue() |
| 51 |
|
| 52 |
|
| 53 |
def test_includes_progress_in_verbose_mode(): |
| 54 |
with capture_verbosity() as o: |
| 55 |
minimal( |
| 56 |
lists(integers(), min_size=1), |
| 57 |
lambda x: sum(x) >= 100, |
| 58 |
settings(verbosity=Verbosity.verbose), |
| 59 |
) |
| 60 |
out = o.getvalue() |
| 61 |
assert out |
| 62 |
assert "Trying example: " in out |
| 63 |
|
| 64 |
|
| 65 |
@xfail_on_crosshair(Why.symbolic_outside_context, strict=False) |
| 66 |
def test_prints_initial_attempts_on_find(): |
| 67 |
with capture_verbosity() as o: |
| 68 |
|
| 69 |
def foo(): |
| 70 |
seen = [] |
| 71 |
|
| 72 |
def not_first(x): |
| 73 |
if not seen: |
| 74 |
seen.append(x) |
| 75 |
return False |
| 76 |
return x not in seen |
| 77 |
|
| 78 |
find( |
| 79 |
integers(), |
| 80 |
not_first, |
| 81 |
settings=settings(verbosity=Verbosity.verbose, max_examples=1000), |
| 82 |
) |
| 83 |
|
| 84 |
foo() |
| 85 |
|
| 86 |
assert "Trying example" in o.getvalue() |
| 87 |
|
| 88 |
|
| 89 |
def test_includes_intermediate_results_in_verbose_mode(): |
| 90 |
with capture_verbosity() as o: |
| 91 |
|
| 92 |
@fails |
| 93 |
@settings( |
| 94 |
verbosity=Verbosity.verbose, |
| 95 |
database=None, |
| 96 |
derandomize=True, |
| 97 |
max_examples=100, |
| 98 |
) |
| 99 |
@given(lists(integers(), min_size=1)) |
| 100 |
def test_foo(x): |
| 101 |
assert sum(x) < 10000 |
| 102 |
|
| 103 |
test_foo() |
| 104 |
lines = o.getvalue().splitlines() |
| 105 |
assert len([l for l in lines if "example" in l]) > 2 |
| 106 |
assert [l for l in lines if "AssertionError" in l] |
| 107 |
|
| 108 |
|
| 109 |
@example(0) |
| 110 |
@settings(verbosity=Verbosity.quiet) |
| 111 |
@given(integers()) |
| 112 |
def test_no_indexerror_in_quiet_mode(x): |
| 113 |
# Regression tests for https://github.com/HypothesisWorks/hypothesis/issues/2696 |
| 114 |
# where quiet mode -> no fragments to report -> IndexError accessing first report |
| 115 |
pass |
| 116 |
|
| 117 |
|
| 118 |
@fails |
| 119 |
@example(0) |
| 120 |
@settings(verbosity=Verbosity.quiet, report_multiple_bugs=True) |
| 121 |
@given(integers()) |
| 122 |
def test_no_indexerror_in_quiet_mode_report_multiple(x): |
| 123 |
assert x |
| 124 |
|
| 125 |
|
| 126 |
@fails |
| 127 |
@example(0) |
| 128 |
@settings(verbosity=Verbosity.quiet, report_multiple_bugs=False) |
| 129 |
@given(integers()) |
| 130 |
def test_no_indexerror_in_quiet_mode_report_one(x): |
| 131 |
assert x |