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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
# 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 st
from hypothesis.database import InMemoryExampleDatabase
from hypothesis.errors import InvalidArgument, NoSuchExample, Unsatisfiable
def test_stops_after_max_examples_if_satisfying():
count = 0
def track(x):
nonlocal count
count += 1
return False
max_examples = 100
with pytest.raises(NoSuchExample):
find(st.integers(0, 10000), track, settings=settings(max_examples=max_examples))
assert count == max_examples
def test_stops_after_ten_times_max_examples_if_not_satisfying():
count = 0
def track(x):
nonlocal count
count += 1
reject()
max_examples = 100
with pytest.raises(Unsatisfiable):
find(st.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 <= 10 * max_examples
some_normal_settings = settings()
def test_is_not_normally_default():
assert settings.default is not some_normal_settings
@given(st.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(st.integers())
def inner(x):
nonlocal final_value
final_value = x
if x > 100:
pytest.fail(f"{x=} is too big!")
with pytest.raises(Failed):
inner()
assert final_value == 101
def test_pytest_skip_skips_shrinking():
seen_large = False
@settings(derandomize=True, max_examples=100)
@given(st.integers())
def inner(x):
nonlocal seen_large
if x > 100:
if seen_large:
raise Exception("Should never replay a skipped test!")
seen_large = True
pytest.skip(f"{x=} is too big!")
with pytest.raises(Skipped):
inner()
def test_can_find_with_db_eq_none():
find(st.integers(), bool, settings=settings(database=None, max_examples=100))
def test_no_such_example():
with pytest.raises(NoSuchExample):
find(st.none(), bool, database_key=b"no such example")
def test_validates_strategies_for_test_method():
invalid_strategy = st.lists(st.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(st.integers())
@settings(phases=[Phase.target, Phase.shrink, Phase.explain])
def no_phases(_):
raise Exception
@given(st.integers())
@settings(phases=[Phase.explicit])
def no_explicit(_):
raise Exception
@given(st.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()
@pytest.mark.parametrize(
"codec, max_codepoint, exclude_categories, categories",
[
("ascii", None, None, None),
("ascii", 128, None, None),
("ascii", 100, None, None),
("utf-8", None, None, None),
("utf-8", None, ["Cs"], None),
("utf-8", None, ["N"], None),
("utf-8", None, None, ["N"]),
],
)
@given(st.data())
def test_characters_codec(codec, max_codepoint, exclude_categories, categories, data):
strategy = st.characters(
codec=codec,
max_codepoint=max_codepoint,
exclude_categories=exclude_categories,
categories=categories,
)
example = data.draw(strategy)
assert example.encode(encoding=codec).decode(encoding=codec) == example
|