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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# 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 traceback
import pytest
from hypothesis import given, settings, strategies as st
from tests.common.utils import fails_with
def fails_with_output(expected):
def _inner(f):
def _new():
with pytest.raises(AssertionError) as err:
f()
if not hasattr(err.value, "__notes__"):
traceback.print_exception(err.value)
raise Exception(
"err.value does not have __notes__, something has gone "
"deeply wrong in the internals"
)
got = "\n".join(err.value.__notes__).strip() + "\n"
assert got == expected.strip() + "\n"
return _new
return _inner
@fails_with_output(
"""
Falsifying example: test_inquisitor_comments_basic_fail_if_either(
# The test always failed when commented parts were varied together.
a=False, # or any other generated value
b=True,
c=[], # or any other generated value
d=True,
e=False, # or any other generated value
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.booleans(), st.booleans(), st.lists(st.none()), st.booleans(), st.booleans())
def test_inquisitor_comments_basic_fail_if_either(a, b, c, d, e):
assert not (b and d)
@fails_with_output(
"""
Falsifying example: test_inquisitor_comments_basic_fail_if_not_all(
# The test sometimes passed when commented parts were varied together.
a='', # or any other generated value
b='', # or any other generated value
c='', # or any other generated value
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.text(), st.text(), st.text())
def test_inquisitor_comments_basic_fail_if_not_all(a, b, c):
condition = a and b and c
assert condition
@fails_with_output(
"""
Falsifying example: test_inquisitor_no_together_comment_if_single_argument(
a='',
b='', # or any other generated value
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.text(), st.text())
def test_inquisitor_no_together_comment_if_single_argument(a, b):
assert a
@st.composite
def ints_with_forced_draw(draw):
data = draw(st.data())
n = draw(st.integers())
data.conjecture_data.draw_boolean(forced=True)
return n
@fails_with_output(
"""
Falsifying example: test_inquisitor_doesnt_break_on_varying_forced_nodes(
n1=100,
n2=0, # or any other generated value
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.integers(), ints_with_forced_draw())
def test_inquisitor_doesnt_break_on_varying_forced_nodes(n1, n2):
assert n1 < 100
@fails_with(ZeroDivisionError)
@settings(database=None)
@given(start_date=st.datetimes(), data=st.data())
def test_issue_3755_regression(start_date, data):
data.draw(st.datetimes(min_value=start_date))
raise ZeroDivisionError
# Tests for sub-argument explanations
class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y
@fails_with_output(
"""
Falsifying example: test_inquisitor_builds_subargs(
obj=MyClass(
0, # or any other generated value
True,
),
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.builds(MyClass, st.integers(), st.booleans()))
def test_inquisitor_builds_subargs(obj):
assert not obj.y
@fails_with_output(
"""
Falsifying example: test_inquisitor_builds_kwargs_subargs(
obj=MyClass(
x=0, # or any other generated value
y=True,
),
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.builds(MyClass, x=st.integers(), y=st.booleans()))
def test_inquisitor_builds_kwargs_subargs(obj):
assert not obj.y
@fails_with_output(
"""
Falsifying example: test_inquisitor_tuple_subargs(
t=(
0, # or any other generated value
True,
),
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.tuples(st.integers(), st.booleans()))
def test_inquisitor_tuple_subargs(t):
assert not t[1]
@fails_with_output(
"""
Falsifying example: test_inquisitor_fixeddict_subargs(
d={
'x': 0, # or any other generated value
'y': True,
},
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.fixed_dictionaries({"x": st.integers(), "y": st.booleans()}))
def test_inquisitor_fixeddict_subargs(d):
assert not d["y"]
@fails_with_output(
"""
Falsifying example: test_inquisitor_tuple_multiple_varying(
t=(
0, # or any other generated value
'', # or any other generated value
True,
),
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.tuples(st.integers(), st.text(), st.booleans()))
def test_inquisitor_tuple_multiple_varying(t):
# Multiple sub-arguments can vary, but the "together" comment only applies
# to top-level test arguments, not to sub-arguments within composites.
assert not t[2]
@fails_with_output(
"""
Falsifying example: test_inquisitor_skip_subset_slices(
obj=MyClass(
(0, False), # or any other generated value
y=False,
),
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(st.builds(MyClass, st.tuples(st.integers(), st.booleans()), y=st.booleans()))
def test_inquisitor_skip_subset_slices(obj):
# The tuple can vary freely, but the booleans inside it shouldn't
# get individual comments since they're part of a larger varying slice.
assert obj.y
# Test for duplicate param names at different nesting levels
@fails_with_output(
"""
Falsifying example: test_inquisitor_duplicate_param_names(
kw=0, # or any other generated value
b={
'kw': '', # or any other generated value
'c': True,
},
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(kw=st.integers(), b=st.fixed_dictionaries({"kw": st.text(), "c": st.booleans()}))
def test_inquisitor_duplicate_param_names(kw, b):
# Both "kw" (top-level) and "b['kw']" can vary - they get separate comments
# even though they have the same name at different nesting levels.
# b['c'] is the critical value that determines the failure.
assert not b["c"]
# Test for multi-level nesting: bare, nested, and double-nested
class Outer:
def __init__(self, inner, value):
self.inner = inner
self.value = value
class Inner:
def __init__(self, x):
self.x = x
@fails_with_output(
"""
Falsifying example: test_inquisitor_multi_level_nesting(
bare=0, # or any other generated value
outer=Outer(
inner=Inner(x=0), # or any other generated value
value=True,
),
)
"""
)
@settings(print_blob=False, derandomize=True)
@given(
bare=st.integers(),
outer=st.builds(
Outer, inner=st.builds(Inner, x=st.integers()), value=st.booleans()
),
)
def test_inquisitor_multi_level_nesting(bare, outer):
# Comments on: bare (top-level) and outer.inner (nested). The inner.x slice
# is the same as inner's slice since Inner has only one argument, so the
# comment appears only once on inner. outer.value doesn't get a comment
# because it's the critical value that determines the failure.
assert not outer.value
|