File: test_given_error_conditions.py

package info (click to toggle)
python-hypothesis 6.67.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,220 kB
  • sloc: python: 46,711; ruby: 1,107; sh: 255; xml: 140; makefile: 49; javascript: 12
file content (114 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download
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
# 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 pytest

from hypothesis import assume, given, reject, settings
from hypothesis.errors import InvalidArgument, Unsatisfiable
from hypothesis.strategies import booleans, integers, nothing

from tests.common.utils import fails_with


def test_raises_unsatisfiable_if_all_false_in_finite_set():
    @given(booleans())
    def test_assume_false(x):
        reject()

    with pytest.raises(Unsatisfiable):
        test_assume_false()


def test_does_not_raise_unsatisfiable_if_some_false_in_finite_set():
    @given(booleans())
    def test_assume_x(x):
        assume(x)

    test_assume_x()


def test_raises_unsatisfiable_if_passed_explicit_nothing():
    @given(x=nothing())
    def test_never_runs(x):
        raise Exception("Can't ever execute this")

    with pytest.raises(
        Unsatisfiable,
        match=r"Cannot generate examples from empty strategy: x=nothing\(\)",
    ):
        test_never_runs()


def test_error_if_has_no_hints():
    @given(a=...)
    def inner(a):
        pass

    with pytest.raises(InvalidArgument):
        inner()


def test_error_if_infer_all_and_has_no_hints():
    @given(...)
    def inner(a):
        pass

    with pytest.raises(InvalidArgument):
        inner()


def test_error_if_infer_is_posarg():
    @given(..., ...)
    def inner(ex1: int, ex2: int):
        pass

    with pytest.raises(InvalidArgument):
        inner()


def test_error_if_infer_is_posarg_mixed_with_kwarg():
    @given(..., ex2=...)
    def inner(ex1: int, ex2: int):
        pass

    with pytest.raises(InvalidArgument):
        inner()


def test_given_twice_is_an_error():
    @settings(deadline=None)
    @given(booleans())
    @given(integers())
    def inner(a, b):
        pass

    with pytest.raises(InvalidArgument):
        inner()


@fails_with(InvalidArgument)
def test_given_is_not_a_class_decorator():
    @given(integers())
    class test_given_is_not_a_class_decorator:
        def __init__(self, i):
            pass


def test_specific_error_for_coroutine_functions():
    @settings(database=None)
    @given(booleans())
    async def foo(x):
        pass

    with pytest.raises(
        InvalidArgument,
        match="Hypothesis doesn't know how to run async test functions",
    ):
        foo()