1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import unittest
import pytest
from _pytest.outcomes import Failed, Skipped
from hypothesis import Phase, example, find, given, reject, settings, strategies as s
from hypothesis.database import InMemoryExampleDatabase
from hypothesis.errors import InvalidArgument, NoSuchExample, Unsatisfiable
def test_stops_after_max_examples_if_satisfying():
tracker = []
def track(x):
tracker.append(x)
return False
max_examples = 100
with pytest.raises(NoSuchExample):
find(s.integers(0, 10000), track, settings=settings(max_examples=max_examples))
assert len(tracker) == max_examples
def test_stops_after_ten_times_max_examples_if_not_satisfying():
count = [0]
def track(x):
count[0] += 1
reject()
max_examples = 100
with pytest.raises(Unsatisfiable):
find(s.integers(0, 10000), track, settings=settings(max_examples=max_examples))
# Very occasionally we can generate overflows in generation, which also
# count towards our example budget, which means that we don't hit the
# maximum.
assert count[0] <= 10 * max_examples
some_normal_settings = settings()
def test_is_not_normally_default():
assert settings.default is not some_normal_settings
@given(s.booleans())
@some_normal_settings
def test_settings_are_default_in_given(x):
assert settings.default is some_normal_settings
def test_given_shrinks_pytest_helper_errors():
final_value = [None]
@settings(derandomize=True, max_examples=100)
@given(s.integers())
def inner(x):
final_value[0] = x
if x > 100:
pytest.fail(f"x={x!r} is too big!")
with pytest.raises(Failed):
inner()
assert final_value[0] == 101
def test_pytest_skip_skips_shrinking():
values = []
@settings(derandomize=True, max_examples=100)
@given(s.integers())
def inner(x):
values.append(x)
if x > 100:
pytest.skip(f"x={x!r} is too big!")
with pytest.raises(Skipped):
inner()
assert len([x for x in values if x > 100]) == 1
def test_can_find_with_db_eq_none():
find(s.integers(), bool, settings=settings(database=None, max_examples=100))
def test_no_such_example():
with pytest.raises(NoSuchExample):
find(s.none(), bool, database_key=b"no such example")
def test_validates_strategies_for_test_method():
invalid_strategy = s.lists(s.nothing(), min_size=1)
class TestStrategyValidation:
@given(invalid_strategy)
def test_method_with_bad_strategy(self, x):
pass
instance = TestStrategyValidation()
with pytest.raises(InvalidArgument):
instance.test_method_with_bad_strategy()
@example(1)
@given(s.integers())
@settings(phases=[Phase.target, Phase.shrink, Phase.explain])
def no_phases(_):
raise Exception
@given(s.integers())
@settings(phases=[Phase.explicit])
def no_explicit(_):
raise Exception
@given(s.integers())
@settings(phases=[Phase.reuse], database=InMemoryExampleDatabase())
def empty_db(_):
raise Exception
@pytest.mark.parametrize(
"test_fn",
[no_phases, no_explicit, empty_db],
ids=lambda t: t.__name__,
)
def test_non_executed_tests_raise_skipped(test_fn):
with pytest.raises(unittest.SkipTest):
test_fn()
|