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
|
# 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/.
from unittest.mock import Mock, create_autospec
import pytest
from hypothesis import example, given
from hypothesis.strategies import integers
from tests.common.utils import fails, skipif_threading
pytest_plugins = "pytester"
@pytest.fixture(scope="session")
def infinity():
return float("inf")
@pytest.fixture(scope="module")
def mock_fixture():
return Mock()
@pytest.fixture(scope="module")
def spec_fixture():
class Foo:
def __init__(self):
pass
def bar(self):
return "baz"
return create_autospec(Foo)
@given(integers())
def test_can_mix_fixture_and_positional_strategy(infinity, xs):
# Hypothesis fills arguments from the right, so if @given() uses
# positional arguments then any strategies need to be on the right.
assert xs <= infinity
@given(xs=integers())
def test_can_mix_fixture_and_keyword_strategy(xs, infinity):
assert xs <= infinity
@example(xs=0)
@given(xs=integers())
def test_can_mix_fixture_example_and_keyword_strategy(xs, infinity):
assert xs <= infinity
@fails
@given(integers())
def test_can_inject_mock_via_fixture(mock_fixture, xs):
"""A negative test is better for this one - this condition uncovers a bug
whereby the mock fixture is executed instead of the test body and always
succeeds. If this test fails, then we know we've run the test body instead
of the mock.
"""
raise AssertionError
@given(integers())
def test_can_inject_autospecced_mock_via_fixture(spec_fixture, xs):
spec_fixture.bar.return_value = float("inf")
assert xs <= spec_fixture.bar()
TESTSUITE = """
import pytest
from hypothesis import given, strategies as st
@pytest.fixture(scope="function", autouse=True)
def autofix(request):
pass
@given(x=st.integers())
def test_requests_function_scoped_fixture(capsys, x):
pass
@pytest.mark.parametrize("percent", ["%", "%s"])
@given(x=st.integers())
def test_requests_function_scoped_fixture_percent_parametrized(capsys, x, percent):
# See https://github.com/HypothesisWorks/hypothesis/issues/2469
pass
class TestClass:
@given(x=st.integers())
def test_requests_method_scoped_fixture(capsys, x):
pass
@given(x=st.integers())
def test_autouse_function_scoped_fixture(x):
pass
"""
def test_given_plus_function_scoped_non_autouse_fixtures_are_deprecated(testdir):
script = testdir.makepyfile(TESTSUITE)
testdir.runpytest(script).assert_outcomes(passed=1, failed=4)
CONFTEST_SUPPRESS = """
from hypothesis import HealthCheck, settings
settings.register_profile(
"suppress",
suppress_health_check=[HealthCheck.function_scoped_fixture],
)
"""
@skipif_threading
def test_suppress_fixture_health_check_via_profile(testdir):
script = testdir.makepyfile(TESTSUITE)
testdir.makeconftest(CONFTEST_SUPPRESS)
testdir.runpytest(script).assert_outcomes(passed=1, failed=4)
testdir.runpytest(script, "--hypothesis-profile=suppress").assert_outcomes(passed=5)
TESTSCRIPT_SUPPRESS_FIXTURE = """
import pytest
from hypothesis import HealthCheck, given, settings, strategies as st
@given(x=st.integers())
def test_fails_health_check(capsys, x):
pass
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
@given(x=st.integers())
def test_suppresses_health_check(capsys, x):
pass
@given(x=st.integers())
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
def test_suppresses_health_check_2(capsys, x):
pass
"""
def test_suppress_health_check_function_scoped_fixture(testdir):
script = testdir.makepyfile(TESTSCRIPT_SUPPRESS_FIXTURE)
testdir.runpytest(script).assert_outcomes(passed=2, failed=1)
TESTSCRIPT_OVERRIDE_FIXTURE = """
import pytest
from hypothesis import given, strategies as st
@pytest.fixture(scope="function", name="event_loop")
def event_loop_1():
return
@pytest.fixture(scope="module", name="event_loop")
def event_loop_2():
return
@given(x=st.integers())
def test_override_fixture(event_loop, x):
pass
"""
def test_given_plus_overridden_fixture(testdir):
script = testdir.makepyfile(TESTSCRIPT_OVERRIDE_FIXTURE)
testdir.runpytest(script, "-Werror").assert_outcomes(passed=1, failed=0)
TESTSCRIPT_FIXTURE_THEN_GIVEN = """
import pytest
from hypothesis import given, strategies as st
@given(x=st.integers())
@pytest.fixture()
def test(x):
pass
"""
pytest_version = tuple(map(int, pytest.__version__.split(".")[:2]))
def assert_outcomes(result, *, errors=0, failed=0):
kwargs = {"errors" if pytest_version[0] > 5 else "error": errors}
result.assert_outcomes(failed=failed, **kwargs)
def test_given_fails_if_already_decorated_with_fixture(testdir):
script = testdir.makepyfile(TESTSCRIPT_FIXTURE_THEN_GIVEN)
result = testdir.runpytest(script)
if pytest_version[:2] >= (8, 4):
assert_outcomes(result, errors=1)
else:
assert_outcomes(result, failed=1)
TESTSCRIPT_GIVEN_THEN_FIXTURE = """
import pytest
from hypothesis import given, strategies as st
@pytest.fixture()
@given(x=st.integers())
def test(x):
pass
"""
def test_fixture_errors_if_already_decorated_with_given(testdir):
script = testdir.makepyfile(TESTSCRIPT_GIVEN_THEN_FIXTURE)
assert_outcomes(testdir.runpytest(script), errors=1)
|