| 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 pytest |
| 12 |
|
| 13 |
from hypothesis import example, given, strategies as st |
| 14 |
from hypothesis.errors import InvalidArgument |
| 15 |
|
| 16 |
from tests.common.utils import fails_with |
| 17 |
|
| 18 |
|
| 19 |
@example(False).via("Manually specified") |
| 20 |
@given(st.booleans()) |
| 21 |
def test_ok_example_via(x): |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
def test_invalid_example_via(): |
| 26 |
with pytest.raises(InvalidArgument): |
| 27 |
example(x=False).via(100) # not a string! |
| 28 |
with pytest.raises(TypeError): |
| 29 |
example(x=False).via("abc", "def") # too many args |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
"kw", |
| 34 |
[ |
| 35 |
{"condition": None}, # must be a bool |
| 36 |
{"reason": None}, # must be a string |
| 37 |
{"raises": None}, # not a BaseException (or even a type) |
| 38 |
{"raises": int}, # not a BaseException |
| 39 |
{"raises": [Exception]}, # not a tuple |
| 40 |
{"raises": (None,)}, # tuple containing a non-BaseException |
| 41 |
{"raises": ()}, # empty tuple doesn't make sense here |
| 42 |
# raising non-failure exceptions, eg KeyboardInterrupt, is tested below |
| 43 |
], |
| 44 |
ids=repr, |
| 45 |
) |
| 46 |
def test_invalid_example_xfail_arguments(kw): |
| 47 |
with pytest.raises(InvalidArgument): |
| 48 |
example(x=False).xfail(**kw) |
| 49 |
|
| 50 |
|
| 51 |
@example(True).xfail() |
| 52 |
@example(True).xfail(reason="ignored for passing tests") |
| 53 |
@example(True).xfail(raises=KeyError) |
| 54 |
@example(True).xfail(raises=(KeyError, ValueError)) |
| 55 |
@example(True).xfail(True, reason="...") |
| 56 |
@example(False).xfail(condition=False) |
| 57 |
@given(st.none()) |
| 58 |
def test_many_xfail_example_decorators(fails): |
| 59 |
if fails: |
| 60 |
raise KeyError |
| 61 |
|
| 62 |
|
| 63 |
@fails_with(AssertionError) |
| 64 |
@example(x=True).xfail(raises=KeyError) |
| 65 |
@given(st.none()) |
| 66 |
def test_xfail_reraises_non_specified_exception(x): |
| 67 |
assert not x |
| 68 |
|
| 69 |
|
| 70 |
@fails_with( |
| 71 |
InvalidArgument, |
| 72 |
match=r"@example\(x=True\) raised an expected BaseException\('msg'\), " |
| 73 |
r"but Hypothesis does not treat this as a test failure", |
| 74 |
) |
| 75 |
@example(True).xfail() |
| 76 |
@given(st.none()) |
| 77 |
def test_must_raise_a_failure_exception(x): |
| 78 |
if x: |
| 79 |
raise BaseException("msg") |
| 80 |
|
| 81 |
|
| 82 |
@fails_with( |
| 83 |
AssertionError, |
| 84 |
match=r"Expected an exception from @example\(x=None\), but no exception was raised.", |
| 85 |
) |
| 86 |
@example(None).xfail() |
| 87 |
@given(st.none()) |
| 88 |
def test_error_on_unexpected_pass_base(x): |
| 89 |
pass |
| 90 |
|
| 91 |
|
| 92 |
@fails_with( |
| 93 |
AssertionError, |
| 94 |
match=r"Expected an AssertionError from @example\(x=None\), but no exception was raised.", |
| 95 |
) |
| 96 |
@example(None).xfail(raises=AssertionError) |
| 97 |
@given(st.none()) |
| 98 |
def test_error_on_unexpected_pass_single(x): |
| 99 |
pass |
| 100 |
|
| 101 |
|
| 102 |
@fails_with( |
| 103 |
AssertionError, |
| 104 |
match=r"Expected an AssertionError from @example\(x=None\), but no exception was raised.", |
| 105 |
) |
| 106 |
@example(None).xfail(raises=(AssertionError,)) |
| 107 |
@given(st.none()) |
| 108 |
def test_error_on_unexpected_pass_single_elem_tuple(x): |
| 109 |
pass |
| 110 |
|
| 111 |
|
| 112 |
@fails_with( |
| 113 |
AssertionError, |
| 114 |
match=r"Expected a KeyError, or ValueError from @example\(x=None\), but no exception was raised.", |
| 115 |
) |
| 116 |
@example(None).xfail(raises=(KeyError, ValueError)) |
| 117 |
@given(st.none()) |
| 118 |
def test_error_on_unexpected_pass_multi(x): |
| 119 |
pass |