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
|
# 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
|