| 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 re |
| 12 |
import time |
| 13 |
import traceback |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import ( |
| 18 |
HealthCheck, |
| 19 |
assume, |
| 20 |
event, |
| 21 |
example, |
| 22 |
given, |
| 23 |
reject, |
| 24 |
settings, |
| 25 |
stateful, |
| 26 |
strategies as st, |
| 27 |
target, |
| 28 |
) |
| 29 |
from hypothesis.control import current_build_context |
| 30 |
from hypothesis.statistics import collector, describe_statistics |
| 31 |
|
| 32 |
from tests.common.utils import Why, xfail_on_crosshair |
| 33 |
|
| 34 |
|
| 35 |
def call_for_statistics(test_function): |
| 36 |
result = [] |
| 37 |
with collector.with_value(result.append): |
| 38 |
try: |
| 39 |
test_function() |
| 40 |
except Exception: |
| 41 |
traceback.print_exc() |
| 42 |
assert len(result) == 1, result |
| 43 |
return result[0] |
| 44 |
|
| 45 |
|
| 46 |
def unique_events(stats): |
| 47 |
return set(sum((t["events"] for t in stats["generate-phase"]["test-cases"]), [])) |
| 48 |
|
| 49 |
|
| 50 |
def test_notes_hard_to_satisfy(): |
| 51 |
@given(st.integers()) |
| 52 |
@settings(suppress_health_check=list(HealthCheck)) |
| 53 |
def test(i): |
| 54 |
assume(i == 13) |
| 55 |
|
| 56 |
stats = call_for_statistics(test) |
| 57 |
assert "satisfied assumptions" in stats["stopped-because"] |
| 58 |
|
| 59 |
|
| 60 |
def test_can_callback_with_a_string(): |
| 61 |
@given(st.integers()) |
| 62 |
def test(i): |
| 63 |
event("hi") |
| 64 |
|
| 65 |
stats = call_for_statistics(test) |
| 66 |
assert any("hi" in s for s in unique_events(stats)) |
| 67 |
|
| 68 |
|
| 69 |
counter = 0 |
| 70 |
seen = [] |
| 71 |
|
| 72 |
|
| 73 |
class Foo: |
| 74 |
def __eq__(self, other): |
| 75 |
return True |
| 76 |
|
| 77 |
def __ne__(self, other): |
| 78 |
return False |
| 79 |
|
| 80 |
def __hash__(self): |
| 81 |
return 0 |
| 82 |
|
| 83 |
def __str__(self): |
| 84 |
seen.append(self) |
| 85 |
global counter |
| 86 |
counter += 1 |
| 87 |
return f"COUNTER {counter}" |
| 88 |
|
| 89 |
|
| 90 |
def test_formats_are_evaluated_only_once(): |
| 91 |
global counter |
| 92 |
counter = 0 |
| 93 |
|
| 94 |
@given(st.integers()) |
| 95 |
def test(i): |
| 96 |
if current_build_context().data.provider.avoid_realization: |
| 97 |
pytest.skip("event() cache is disabled under avoid_realization = True") |
| 98 |
event(Foo()) |
| 99 |
|
| 100 |
stats = call_for_statistics(test) |
| 101 |
assert "COUNTER 1" in unique_events(stats) |
| 102 |
assert "COUNTER 2" not in unique_events(stats) |
| 103 |
|
| 104 |
|
| 105 |
def test_does_not_report_on_examples(): |
| 106 |
@example("hi") |
| 107 |
@given(st.integers()) |
| 108 |
def test(i): |
| 109 |
if isinstance(i, str): |
| 110 |
event("boo") |
| 111 |
|
| 112 |
stats = call_for_statistics(test) |
| 113 |
assert not unique_events(stats) |
| 114 |
|
| 115 |
|
| 116 |
def test_exact_timing(): |
| 117 |
@settings(suppress_health_check=[HealthCheck.too_slow], deadline=None) |
| 118 |
@given(st.integers()) |
| 119 |
def test(i): |
| 120 |
time.sleep(0.5) |
| 121 |
|
| 122 |
stats = describe_statistics(call_for_statistics(test)) |
| 123 |
assert "~ 500ms" in stats |
| 124 |
|
| 125 |
|
| 126 |
def test_apparently_instantaneous_tests(): |
| 127 |
time.freeze() |
| 128 |
|
| 129 |
@given(st.integers()) |
| 130 |
def test(i): |
| 131 |
pass |
| 132 |
|
| 133 |
stats = describe_statistics(call_for_statistics(test)) |
| 134 |
assert "< 1ms" in stats |
| 135 |
|
| 136 |
|
| 137 |
@xfail_on_crosshair(Why.other) # crosshair re-executes for flakiness itself |
| 138 |
def test_flaky_exit(): |
| 139 |
first = True |
| 140 |
|
| 141 |
@settings(derandomize=True) |
| 142 |
@given(st.integers()) |
| 143 |
def test(i): |
| 144 |
nonlocal first |
| 145 |
if i > 1001: |
| 146 |
if first: |
| 147 |
first = False |
| 148 |
raise AssertionError |
| 149 |
|
| 150 |
stats = call_for_statistics(test) |
| 151 |
assert stats["stopped-because"] == "test was flaky" |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.parametrize("draw_delay", [False, True]) |
| 155 |
@pytest.mark.parametrize("test_delay", [False, True]) |
| 156 |
def test_draw_timing(draw_delay, test_delay): |
| 157 |
time.freeze() |
| 158 |
|
| 159 |
@st.composite |
| 160 |
def s(draw): |
| 161 |
if draw_delay: |
| 162 |
time.sleep(0.05) |
| 163 |
draw(st.integers()) |
| 164 |
|
| 165 |
@given(s()) |
| 166 |
def test(_): |
| 167 |
if test_delay: |
| 168 |
time.sleep(0.05) |
| 169 |
|
| 170 |
stats = describe_statistics(call_for_statistics(test)) |
| 171 |
if not draw_delay: |
| 172 |
assert "< 1ms" in stats |
| 173 |
else: |
| 174 |
match = re.search(r"of which ~ (?P<gentime>\d+)", stats) |
| 175 |
assert 49 <= int(match.group("gentime")) <= 51 |
| 176 |
|
| 177 |
|
| 178 |
def test_has_lambdas_in_output(): |
| 179 |
@settings(max_examples=100, database=None) |
| 180 |
@given(st.integers().filter(lambda x: x % 2 == 0)) |
| 181 |
def test(i): |
| 182 |
pass |
| 183 |
|
| 184 |
stats = call_for_statistics(test) |
| 185 |
assert any("lambda x: x % 2 == 0" in e for e in unique_events(stats)) |
| 186 |
|
| 187 |
|
| 188 |
def test_stops_after_x_shrinks(monkeypatch): |
| 189 |
# the max_shrinks argument is deprecated, but we still stop after some |
| 190 |
# number - which we can reduce to zero to check that this works. |
| 191 |
from hypothesis.internal.conjecture import engine |
| 192 |
|
| 193 |
monkeypatch.setattr(engine, "MAX_SHRINKS", 0) |
| 194 |
|
| 195 |
@given(st.integers(min_value=0)) |
| 196 |
def test(n): |
| 197 |
assert n < 10 |
| 198 |
|
| 199 |
stats = call_for_statistics(test) |
| 200 |
assert "shrunk example" in stats["stopped-because"] |
| 201 |
|
| 202 |
|
| 203 |
def test_stateful_states_are_deduped(): |
| 204 |
class DemoStateMachine(stateful.RuleBasedStateMachine): |
| 205 |
Stuff = stateful.Bundle("stuff") |
| 206 |
|
| 207 |
@stateful.rule(target=Stuff, name=st.text()) |
| 208 |
def create_stuff(self, name): |
| 209 |
return name |
| 210 |
|
| 211 |
@stateful.rule(item=Stuff) |
| 212 |
def do(self, item): |
| 213 |
return |
| 214 |
|
| 215 |
stats = call_for_statistics(DemoStateMachine.TestCase().runTest) |
| 216 |
stats = unique_events(stats) |
| 217 |
stats = [s for s in stats if not s.startswith("invalid because: (internal)")] |
| 218 |
assert len(stats) <= 2 |
| 219 |
|
| 220 |
|
| 221 |
def test_stateful_with_one_of_bundles_states_are_deduped(): |
| 222 |
class DemoStateMachine(stateful.RuleBasedStateMachine): |
| 223 |
Things = stateful.Bundle("things") |
| 224 |
Stuff = stateful.Bundle("stuff") |
| 225 |
StuffAndThings = Things | Stuff |
| 226 |
|
| 227 |
@stateful.rule(target=Things, name=st.text()) |
| 228 |
def create_thing(self, name): |
| 229 |
return name |
| 230 |
|
| 231 |
@stateful.rule(target=Stuff, name=st.text()) |
| 232 |
def create_stuff(self, name): |
| 233 |
return name |
| 234 |
|
| 235 |
@stateful.rule(item=StuffAndThings) |
| 236 |
def do(self, item): |
| 237 |
return |
| 238 |
|
| 239 |
stats = call_for_statistics(DemoStateMachine.TestCase().runTest) |
| 240 |
stats = unique_events(stats) |
| 241 |
stats = [s for s in stats if not s.startswith("invalid because: (internal)")] |
| 242 |
assert len(stats) <= 4 |
| 243 |
|
| 244 |
|
| 245 |
def test_statistics_for_threshold_problem(): |
| 246 |
@settings(max_examples=100, database=None) |
| 247 |
@given(st.floats(min_value=0, allow_infinity=False)) |
| 248 |
def threshold(error): |
| 249 |
target(error, label="error") |
| 250 |
assert error <= 10 |
| 251 |
target(0.0, label="never in failing example") |
| 252 |
|
| 253 |
stats = call_for_statistics(threshold) |
| 254 |
assert " - Highest target scores:" in describe_statistics(stats) |
| 255 |
assert "never in failing example" in describe_statistics(stats) |
| 256 |
# Check that we report far-from-threshold failing examples |
| 257 |
assert stats["targets"]["error"] > 10 |
| 258 |
|
| 259 |
|
| 260 |
def test_statistics_with_events_and_target(): |
| 261 |
@given(st.integers(0, 10_000)) |
| 262 |
def test(value): |
| 263 |
event(value) |
| 264 |
target(float(value), label="a target") |
| 265 |
|
| 266 |
stats = describe_statistics(call_for_statistics(test)) |
| 267 |
assert "- Events:" in stats |
| 268 |
assert "- Highest target score: " in stats |
| 269 |
|
| 270 |
|
| 271 |
@given(st.booleans()) |
| 272 |
def test_event_with_non_weakrefable_keys(b): |
| 273 |
event((b,)) |
| 274 |
|
| 275 |
|
| 276 |
def test_assume_adds_event_with_function_origin(): |
| 277 |
@given(st.integers()) |
| 278 |
def very_distinguishable_name(n): |
| 279 |
assume(n > 100) |
| 280 |
|
| 281 |
stats = call_for_statistics(very_distinguishable_name) |
| 282 |
|
| 283 |
for tc in stats["generate-phase"]["test-cases"]: |
| 284 |
for e in tc["events"]: |
| 285 |
assert "failed to satisfy assume() in very_distinguishable_name" in e |
| 286 |
|
| 287 |
|
| 288 |
def test_reject_adds_event_with_function_origin(): |
| 289 |
@given(st.integers()) |
| 290 |
def very_distinguishable_name(n): |
| 291 |
if n > 100: |
| 292 |
reject() |
| 293 |
|
| 294 |
stats = call_for_statistics(very_distinguishable_name) |
| 295 |
|
| 296 |
for tc in stats["generate-phase"]["test-cases"]: |
| 297 |
for e in tc["events"]: |
| 298 |
assert "reject() in very_distinguishable_name" in e |