| 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 traceback |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import Phase, given, settings, strategies as st |
| 16 |
|
| 17 |
from tests.common.debug import minimal |
| 18 |
from tests.common.utils import fails_with |
| 19 |
|
| 20 |
|
| 21 |
def fails_with_output(expected): |
| 22 |
def _inner(f): |
| 23 |
def _new(): |
| 24 |
with pytest.raises(AssertionError) as err: |
| 25 |
f() |
| 26 |
|
| 27 |
if not hasattr(err.value, "__notes__"): |
| 28 |
traceback.print_exception(err.value) |
| 29 |
raise Exception( |
| 30 |
"err.value does not have __notes__, something has gone " |
| 31 |
"deeply wrong in the internals" |
| 32 |
) |
| 33 |
|
| 34 |
got = "\n".join(err.value.__notes__).strip() + "\n" |
| 35 |
assert got == expected.strip() + "\n" |
| 36 |
|
| 37 |
return _new |
| 38 |
|
| 39 |
return _inner |
| 40 |
|
| 41 |
|
| 42 |
@fails_with_output(""" |
| 43 |
Falsifying example: test_inquisitor_comments_basic_fail_if_either( |
| 44 |
# The test always failed when commented parts were varied together. |
| 45 |
a=False, # or any other generated value |
| 46 |
b=True, |
| 47 |
c=[], # or any other generated value |
| 48 |
d=True, |
| 49 |
e=False, # or any other generated value |
| 50 |
) |
| 51 |
""") |
| 52 |
@settings(print_blob=False, derandomize=True) |
| 53 |
@given(st.booleans(), st.booleans(), st.lists(st.none()), st.booleans(), st.booleans()) |
| 54 |
def test_inquisitor_comments_basic_fail_if_either(a, b, c, d, e): |
| 55 |
assert not (b and d) |
| 56 |
|
| 57 |
|
| 58 |
@fails_with_output(""" |
| 59 |
Falsifying example: test_inquisitor_comments_basic_fail_if_not_all( |
| 60 |
# The test sometimes passed when commented parts were varied together. |
| 61 |
a='', # or any other generated value |
| 62 |
b='', # or any other generated value |
| 63 |
c='', # or any other generated value |
| 64 |
) |
| 65 |
""") |
| 66 |
@settings(print_blob=False, derandomize=True) |
| 67 |
@given(st.text(), st.text(), st.text()) |
| 68 |
def test_inquisitor_comments_basic_fail_if_not_all(a, b, c): |
| 69 |
condition = a and b and c |
| 70 |
assert condition |
| 71 |
|
| 72 |
|
| 73 |
@fails_with_output(""" |
| 74 |
Falsifying example: test_inquisitor_no_together_comment_if_single_argument( |
| 75 |
a='', |
| 76 |
b='', # or any other generated value |
| 77 |
) |
| 78 |
""") |
| 79 |
@settings(print_blob=False, derandomize=True) |
| 80 |
@given(st.text(), st.text()) |
| 81 |
def test_inquisitor_no_together_comment_if_single_argument(a, b): |
| 82 |
assert a |
| 83 |
|
| 84 |
|
| 85 |
@st.composite |
| 86 |
def ints_with_forced_draw(draw): |
| 87 |
data = draw(st.data()) |
| 88 |
n = draw(st.integers()) |
| 89 |
data.conjecture_data.draw_boolean(forced=True) |
| 90 |
return n |
| 91 |
|
| 92 |
|
| 93 |
@fails_with_output(""" |
| 94 |
Falsifying example: test_inquisitor_doesnt_break_on_varying_forced_nodes( |
| 95 |
n1=100, |
| 96 |
n2=0, # or any other generated value |
| 97 |
) |
| 98 |
""") |
| 99 |
@settings(print_blob=False, derandomize=True) |
| 100 |
@given(st.integers(), ints_with_forced_draw()) |
| 101 |
def test_inquisitor_doesnt_break_on_varying_forced_nodes(n1, n2): |
| 102 |
assert n1 < 100 |
| 103 |
|
| 104 |
|
| 105 |
@fails_with(ZeroDivisionError) |
| 106 |
@settings(database=None) |
| 107 |
@given(start_date=st.datetimes(), data=st.data()) |
| 108 |
def test_issue_3755_regression(start_date, data): |
| 109 |
data.draw(st.datetimes(min_value=start_date)) |
| 110 |
raise ZeroDivisionError |
| 111 |
|
| 112 |
|
| 113 |
# Tests for sub-argument explanations |
| 114 |
|
| 115 |
|
| 116 |
class MyClass: |
| 117 |
def __init__(self, x, y): |
| 118 |
self.x = x |
| 119 |
self.y = y |
| 120 |
|
| 121 |
|
| 122 |
@fails_with_output(""" |
| 123 |
Falsifying example: test_inquisitor_builds_subargs( |
| 124 |
obj=MyClass( |
| 125 |
0, # or any other generated value |
| 126 |
True, |
| 127 |
), |
| 128 |
) |
| 129 |
""") |
| 130 |
@settings(print_blob=False, derandomize=True) |
| 131 |
@given(st.builds(MyClass, st.integers(), st.booleans())) |
| 132 |
def test_inquisitor_builds_subargs(obj): |
| 133 |
assert not obj.y |
| 134 |
|
| 135 |
|
| 136 |
@fails_with_output(""" |
| 137 |
Falsifying example: test_inquisitor_builds_kwargs_subargs( |
| 138 |
obj=MyClass( |
| 139 |
x=0, # or any other generated value |
| 140 |
y=True, |
| 141 |
), |
| 142 |
) |
| 143 |
""") |
| 144 |
@settings(print_blob=False, derandomize=True) |
| 145 |
@given(st.builds(MyClass, x=st.integers(), y=st.booleans())) |
| 146 |
def test_inquisitor_builds_kwargs_subargs(obj): |
| 147 |
assert not obj.y |
| 148 |
|
| 149 |
|
| 150 |
@fails_with_output(""" |
| 151 |
Falsifying example: test_inquisitor_tuple_subargs( |
| 152 |
t=( |
| 153 |
0, # or any other generated value |
| 154 |
True, |
| 155 |
), |
| 156 |
) |
| 157 |
""") |
| 158 |
@settings(print_blob=False, derandomize=True) |
| 159 |
@given(st.tuples(st.integers(), st.booleans())) |
| 160 |
def test_inquisitor_tuple_subargs(t): |
| 161 |
assert not t[1] |
| 162 |
|
| 163 |
|
| 164 |
@fails_with_output(""" |
| 165 |
Falsifying example: test_inquisitor_fixeddict_subargs( |
| 166 |
d={ |
| 167 |
'x': 0, # or any other generated value |
| 168 |
'y': True, |
| 169 |
}, |
| 170 |
) |
| 171 |
""") |
| 172 |
@settings(print_blob=False, derandomize=True) |
| 173 |
@given(st.fixed_dictionaries({"x": st.integers(), "y": st.booleans()})) |
| 174 |
def test_inquisitor_fixeddict_subargs(d): |
| 175 |
assert not d["y"] |
| 176 |
|
| 177 |
|
| 178 |
@fails_with_output(""" |
| 179 |
Falsifying example: test_inquisitor_tuple_multiple_varying( |
| 180 |
t=( |
| 181 |
0, # or any other generated value |
| 182 |
'', # or any other generated value |
| 183 |
True, |
| 184 |
), |
| 185 |
) |
| 186 |
""") |
| 187 |
@settings(print_blob=False, derandomize=True) |
| 188 |
@given(st.tuples(st.integers(), st.text(), st.booleans())) |
| 189 |
def test_inquisitor_tuple_multiple_varying(t): |
| 190 |
# Multiple sub-arguments can vary, but the "together" comment only applies |
| 191 |
# to top-level test arguments, not to sub-arguments within composites. |
| 192 |
assert not t[2] |
| 193 |
|
| 194 |
|
| 195 |
@fails_with_output(""" |
| 196 |
Falsifying example: test_inquisitor_skip_subset_slices( |
| 197 |
obj=MyClass( |
| 198 |
(0, False), # or any other generated value |
| 199 |
y=False, |
| 200 |
), |
| 201 |
) |
| 202 |
""") |
| 203 |
@settings(print_blob=False, derandomize=True) |
| 204 |
@given(st.builds(MyClass, st.tuples(st.integers(), st.booleans()), y=st.booleans())) |
| 205 |
def test_inquisitor_skip_subset_slices(obj): |
| 206 |
# The tuple can vary freely, but the booleans inside it shouldn't |
| 207 |
# get individual comments since they're part of a larger varying slice. |
| 208 |
assert obj.y |
| 209 |
|
| 210 |
|
| 211 |
# Test for duplicate param names at different nesting levels |
| 212 |
@fails_with_output(""" |
| 213 |
Falsifying example: test_inquisitor_duplicate_param_names( |
| 214 |
kw=0, # or any other generated value |
| 215 |
b={ |
| 216 |
'kw': '', # or any other generated value |
| 217 |
'c': True, |
| 218 |
}, |
| 219 |
) |
| 220 |
""") |
| 221 |
@settings(print_blob=False, derandomize=True) |
| 222 |
@given(kw=st.integers(), b=st.fixed_dictionaries({"kw": st.text(), "c": st.booleans()})) |
| 223 |
def test_inquisitor_duplicate_param_names(kw, b): |
| 224 |
# Both "kw" (top-level) and "b['kw']" can vary - they get separate comments |
| 225 |
# even though they have the same name at different nesting levels. |
| 226 |
# b['c'] is the critical value that determines the failure. |
| 227 |
assert not b["c"] |
| 228 |
|
| 229 |
|
| 230 |
# Test for multi-level nesting: bare, nested, and double-nested |
| 231 |
class Outer: |
| 232 |
def __init__(self, inner, value): |
| 233 |
self.inner = inner |
| 234 |
self.value = value |
| 235 |
|
| 236 |
|
| 237 |
class Inner: |
| 238 |
def __init__(self, x): |
| 239 |
self.x = x |
| 240 |
|
| 241 |
|
| 242 |
@fails_with_output(""" |
| 243 |
Falsifying example: test_inquisitor_multi_level_nesting( |
| 244 |
bare=0, # or any other generated value |
| 245 |
outer=Outer( |
| 246 |
inner=Inner(x=0), # or any other generated value |
| 247 |
value=True, |
| 248 |
), |
| 249 |
) |
| 250 |
""") |
| 251 |
@settings(print_blob=False, derandomize=True) |
| 252 |
@given( |
| 253 |
bare=st.integers(), |
| 254 |
outer=st.builds( |
| 255 |
Outer, inner=st.builds(Inner, x=st.integers()), value=st.booleans() |
| 256 |
), |
| 257 |
) |
| 258 |
def test_inquisitor_multi_level_nesting(bare, outer): |
| 259 |
# Comments on: bare (top-level) and outer.inner (nested). The inner.x slice |
| 260 |
# is the same as inner's slice since Inner has only one argument, so the |
| 261 |
# comment appears only once on inner. outer.value doesn't get a comment |
| 262 |
# because it's the critical value that determines the failure. |
| 263 |
assert not outer.value |
| 264 |
|
| 265 |
|
| 266 |
def test_explain_does_not_crash_with_per_run_labels(): |
| 267 |
# Regression test for https://github.com/HypothesisWorks/hypothesis/issues/4708. |
| 268 |
# If a strategy constructed inside a composite draws from st.just() of a |
| 269 |
# fresh instance of a user-defined class, the resulting OneOfStrategy has |
| 270 |
# a different label on each test invocation. The explain phase would |
| 271 |
# previously assert equal labels between the shrink target and a replayed |
| 272 |
# result, which is not guaranteed to hold. |
| 273 |
class W: |
| 274 |
def __init__(self, a): |
| 275 |
self.a = a |
| 276 |
|
| 277 |
inner = st.one_of(st.integers(), st.tuples(st.integers(), st.integers())) |
| 278 |
elements = st.tuples(inner, st.just(0)) |
| 279 |
structured = st.lists(elements, min_size=1, max_size=3) |
| 280 |
|
| 281 |
@st.composite |
| 282 |
def wrapped(draw): |
| 283 |
return draw(st.one_of(st.just(W(None)), structured.map(W))) |
| 284 |
|
| 285 |
minimal( |
| 286 |
wrapped(), |
| 287 |
lambda x: isinstance(x.a, list), |
| 288 |
settings=settings(phases=(Phase.generate, Phase.shrink, Phase.explain)), |
| 289 |
) |