| 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 |
from unittest.mock import Mock, create_autospec |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import example, given |
| 16 |
from hypothesis.strategies import integers |
| 17 |
|
| 18 |
from tests.common.utils import fails, skipif_threading |
| 19 |
|
| 20 |
pytest_plugins = "pytester" |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture(scope="session") |
| 24 |
def infinity(): |
| 25 |
return float("inf") |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(scope="module") |
| 29 |
def mock_fixture(): |
| 30 |
return Mock() |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture(scope="module") |
| 34 |
def spec_fixture(): |
| 35 |
class Foo: |
| 36 |
def __init__(self): |
| 37 |
pass |
| 38 |
|
| 39 |
def bar(self): |
| 40 |
return "baz" |
| 41 |
|
| 42 |
return create_autospec(Foo) |
| 43 |
|
| 44 |
|
| 45 |
@given(integers()) |
| 46 |
def test_can_mix_fixture_and_positional_strategy(infinity, xs): |
| 47 |
# Hypothesis fills arguments from the right, so if @given() uses |
| 48 |
# positional arguments then any strategies need to be on the right. |
| 49 |
assert xs <= infinity |
| 50 |
|
| 51 |
|
| 52 |
@given(xs=integers()) |
| 53 |
def test_can_mix_fixture_and_keyword_strategy(xs, infinity): |
| 54 |
assert xs <= infinity |
| 55 |
|
| 56 |
|
| 57 |
@example(xs=0) |
| 58 |
@given(xs=integers()) |
| 59 |
def test_can_mix_fixture_example_and_keyword_strategy(xs, infinity): |
| 60 |
assert xs <= infinity |
| 61 |
|
| 62 |
|
| 63 |
@fails |
| 64 |
@given(integers()) |
| 65 |
def test_can_inject_mock_via_fixture(mock_fixture, xs): |
| 66 |
"""A negative test is better for this one - this condition uncovers a bug |
| 67 |
whereby the mock fixture is executed instead of the test body and always |
| 68 |
succeeds. If this test fails, then we know we've run the test body instead |
| 69 |
of the mock. |
| 70 |
""" |
| 71 |
raise AssertionError |
| 72 |
|
| 73 |
|
| 74 |
@given(integers()) |
| 75 |
def test_can_inject_autospecced_mock_via_fixture(spec_fixture, xs): |
| 76 |
spec_fixture.bar.return_value = float("inf") |
| 77 |
assert xs <= spec_fixture.bar() |
| 78 |
|
| 79 |
|
| 80 |
TESTSUITE = """ |
| 81 |
import pytest |
| 82 |
from hypothesis import given, strategies as st |
| 83 |
|
| 84 |
@pytest.fixture(scope="function", autouse=True) |
| 85 |
def autofix(request): |
| 86 |
pass |
| 87 |
|
| 88 |
@given(x=st.integers()) |
| 89 |
def test_requests_function_scoped_fixture(capsys, x): |
| 90 |
pass |
| 91 |
|
| 92 |
@pytest.mark.parametrize("percent", ["%", "%s"]) |
| 93 |
@given(x=st.integers()) |
| 94 |
def test_requests_function_scoped_fixture_percent_parametrized(capsys, x, percent): |
| 95 |
# See https://github.com/HypothesisWorks/hypothesis/issues/2469 |
| 96 |
pass |
| 97 |
|
| 98 |
class TestClass: |
| 99 |
@given(x=st.integers()) |
| 100 |
def test_requests_method_scoped_fixture(capsys, x): |
| 101 |
pass |
| 102 |
|
| 103 |
@given(x=st.integers()) |
| 104 |
def test_autouse_function_scoped_fixture(x): |
| 105 |
pass |
| 106 |
""" |
| 107 |
|
| 108 |
|
| 109 |
def test_given_plus_function_scoped_non_autouse_fixtures_are_deprecated(testdir): |
| 110 |
script = testdir.makepyfile(TESTSUITE) |
| 111 |
testdir.runpytest(script).assert_outcomes(passed=1, failed=4) |
| 112 |
|
| 113 |
|
| 114 |
CONFTEST_SUPPRESS = """ |
| 115 |
from hypothesis import HealthCheck, settings |
| 116 |
|
| 117 |
settings.register_profile( |
| 118 |
"suppress", |
| 119 |
suppress_health_check=[HealthCheck.function_scoped_fixture], |
| 120 |
) |
| 121 |
""" |
| 122 |
|
| 123 |
|
| 124 |
@skipif_threading |
| 125 |
def test_suppress_fixture_health_check_via_profile(testdir): |
| 126 |
script = testdir.makepyfile(TESTSUITE) |
| 127 |
testdir.makeconftest(CONFTEST_SUPPRESS) |
| 128 |
|
| 129 |
testdir.runpytest(script).assert_outcomes(passed=1, failed=4) |
| 130 |
testdir.runpytest(script, "--hypothesis-profile=suppress").assert_outcomes(passed=5) |
| 131 |
|
| 132 |
|
| 133 |
TESTSCRIPT_SUPPRESS_FIXTURE = """ |
| 134 |
import pytest |
| 135 |
from hypothesis import HealthCheck, given, settings, strategies as st |
| 136 |
|
| 137 |
@given(x=st.integers()) |
| 138 |
def test_fails_health_check(capsys, x): |
| 139 |
pass |
| 140 |
|
| 141 |
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture]) |
| 142 |
@given(x=st.integers()) |
| 143 |
def test_suppresses_health_check(capsys, x): |
| 144 |
pass |
| 145 |
|
| 146 |
@given(x=st.integers()) |
| 147 |
@settings(suppress_health_check=[HealthCheck.function_scoped_fixture]) |
| 148 |
def test_suppresses_health_check_2(capsys, x): |
| 149 |
pass |
| 150 |
""" |
| 151 |
|
| 152 |
|
| 153 |
def test_suppress_health_check_function_scoped_fixture(testdir): |
| 154 |
script = testdir.makepyfile(TESTSCRIPT_SUPPRESS_FIXTURE) |
| 155 |
testdir.runpytest(script).assert_outcomes(passed=2, failed=1) |
| 156 |
|
| 157 |
|
| 158 |
TESTSCRIPT_OVERRIDE_FIXTURE = """ |
| 159 |
import pytest |
| 160 |
from hypothesis import given, strategies as st |
| 161 |
|
| 162 |
@pytest.fixture(scope="function", name="event_loop") |
| 163 |
def event_loop_1(): |
| 164 |
return |
| 165 |
|
| 166 |
@pytest.fixture(scope="module", name="event_loop") |
| 167 |
def event_loop_2(): |
| 168 |
return |
| 169 |
|
| 170 |
@given(x=st.integers()) |
| 171 |
def test_override_fixture(event_loop, x): |
| 172 |
pass |
| 173 |
""" |
| 174 |
|
| 175 |
|
| 176 |
def test_given_plus_overridden_fixture(testdir): |
| 177 |
script = testdir.makepyfile(TESTSCRIPT_OVERRIDE_FIXTURE) |
| 178 |
testdir.runpytest(script, "-Werror").assert_outcomes(passed=1, failed=0) |
| 179 |
|
| 180 |
|
| 181 |
TESTSCRIPT_FIXTURE_THEN_GIVEN = """ |
| 182 |
import pytest |
| 183 |
from hypothesis import given, strategies as st |
| 184 |
|
| 185 |
@given(x=st.integers()) |
| 186 |
@pytest.fixture() |
| 187 |
def test(x): |
| 188 |
pass |
| 189 |
""" |
| 190 |
|
| 191 |
pytest_version = tuple(map(int, pytest.__version__.split(".")[:2])) |
| 192 |
|
| 193 |
|
| 194 |
def assert_outcomes(result, *, errors=0, failed=0): |
| 195 |
kwargs = {"errors" if pytest_version[0] > 5 else "error": errors} |
| 196 |
result.assert_outcomes(failed=failed, **kwargs) |
| 197 |
|
| 198 |
|
| 199 |
def test_given_fails_if_already_decorated_with_fixture(testdir): |
| 200 |
script = testdir.makepyfile(TESTSCRIPT_FIXTURE_THEN_GIVEN) |
| 201 |
result = testdir.runpytest(script) |
| 202 |
if pytest_version[:2] >= (8, 4): |
| 203 |
assert_outcomes(result, errors=1) |
| 204 |
else: |
| 205 |
assert_outcomes(result, failed=1) |
| 206 |
|
| 207 |
|
| 208 |
TESTSCRIPT_GIVEN_THEN_FIXTURE = """ |
| 209 |
import pytest |
| 210 |
from hypothesis import given, strategies as st |
| 211 |
|
| 212 |
@pytest.fixture() |
| 213 |
@given(x=st.integers()) |
| 214 |
def test(x): |
| 215 |
pass |
| 216 |
""" |
| 217 |
|
| 218 |
|
| 219 |
def test_fixture_errors_if_already_decorated_with_given(testdir): |
| 220 |
script = testdir.makepyfile(TESTSCRIPT_GIVEN_THEN_FIXTURE) |
| 221 |
assert_outcomes(testdir.runpytest(script), errors=1) |