| 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 assume, given, reject, settings |
| 14 |
from hypothesis._settings import all_settings |
| 15 |
from hypothesis.errors import InvalidArgument, Unsatisfiable |
| 16 |
from hypothesis.strategies import booleans, integers, nothing |
| 17 |
|
| 18 |
from tests.common.utils import fails_with |
| 19 |
|
| 20 |
|
| 21 |
@fails_with(Unsatisfiable) |
| 22 |
@given(booleans()) |
| 23 |
def test_raises_unsatisfiable_if_all_false_in_finite_set(x): |
| 24 |
reject() |
| 25 |
|
| 26 |
|
| 27 |
def test_does_not_raise_unsatisfiable_if_some_false_in_finite_set(): |
| 28 |
@given(booleans()) |
| 29 |
def test_assume_x(x): |
| 30 |
assume(x) |
| 31 |
|
| 32 |
test_assume_x() |
| 33 |
|
| 34 |
|
| 35 |
def test_raises_unsatisfiable_if_passed_explicit_nothing(): |
| 36 |
@given(x=nothing()) |
| 37 |
def test_never_runs(x): |
| 38 |
raise Exception("Can't ever execute this") |
| 39 |
|
| 40 |
with pytest.raises( |
| 41 |
Unsatisfiable, |
| 42 |
match=r"Cannot generate examples from empty strategy: x=nothing\(\)", |
| 43 |
): |
| 44 |
test_never_runs() |
| 45 |
|
| 46 |
|
| 47 |
def test_error_if_has_no_hints(): |
| 48 |
@given(a=...) |
| 49 |
def inner(a): |
| 50 |
pass |
| 51 |
|
| 52 |
with pytest.raises(InvalidArgument): |
| 53 |
inner() |
| 54 |
|
| 55 |
|
| 56 |
def test_error_if_infer_all_and_has_no_hints(): |
| 57 |
@given(...) |
| 58 |
def inner(a): |
| 59 |
pass |
| 60 |
|
| 61 |
with pytest.raises(InvalidArgument): |
| 62 |
inner() |
| 63 |
|
| 64 |
|
| 65 |
def test_error_if_infer_is_posarg(): |
| 66 |
@given(..., ...) |
| 67 |
def inner(ex1: int, ex2: int): |
| 68 |
pass |
| 69 |
|
| 70 |
with pytest.raises(InvalidArgument): |
| 71 |
inner() |
| 72 |
|
| 73 |
|
| 74 |
def test_error_if_infer_is_posarg_mixed_with_kwarg(): |
| 75 |
@given(..., ex2=...) |
| 76 |
def inner(ex1: int, ex2: int): |
| 77 |
pass |
| 78 |
|
| 79 |
with pytest.raises(InvalidArgument): |
| 80 |
inner() |
| 81 |
|
| 82 |
|
| 83 |
def test_given_twice_is_an_error(): |
| 84 |
@settings(deadline=None) |
| 85 |
@given(booleans()) |
| 86 |
@given(integers()) |
| 87 |
def inner(a, b): |
| 88 |
pass |
| 89 |
|
| 90 |
with pytest.raises(InvalidArgument): |
| 91 |
inner() |
| 92 |
|
| 93 |
|
| 94 |
@fails_with(InvalidArgument) |
| 95 |
def test_given_is_not_a_class_decorator(): |
| 96 |
@given(integers()) |
| 97 |
class test_given_is_not_a_class_decorator: |
| 98 |
def __init__(self, i): |
| 99 |
pass |
| 100 |
|
| 101 |
|
| 102 |
def test_specific_error_for_coroutine_functions(): |
| 103 |
@settings(database=None) |
| 104 |
@given(booleans()) |
| 105 |
async def foo(x): |
| 106 |
pass |
| 107 |
|
| 108 |
with pytest.raises( |
| 109 |
InvalidArgument, |
| 110 |
match="Hypothesis doesn't know how to run async test functions", |
| 111 |
): |
| 112 |
foo() |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize("setting_name", all_settings) |
| 116 |
def test_suggests_at_settings_if_extra_kwarg_matches_setting_name(setting_name): |
| 117 |
val = 1 |
| 118 |
|
| 119 |
# dynamically create functions with an extra kwarg argument which happens to |
| 120 |
# match a settings variable. The user probably meant @settings. |
| 121 |
# exec is pretty cursed here, but it does work. |
| 122 |
namespace = {} |
| 123 |
exec( |
| 124 |
f""" |
| 125 |
@given(a=1, {setting_name}={val}) |
| 126 |
def foo(a): |
| 127 |
pass |
| 128 |
""", |
| 129 |
globals(), |
| 130 |
namespace, |
| 131 |
) |
| 132 |
|
| 133 |
with pytest.raises( |
| 134 |
InvalidArgument, |
| 135 |
match=rf"Did you mean @settings\({setting_name}={val}\)\?", |
| 136 |
): |
| 137 |
namespace["foo"]() |