| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from _pytest import deprecated |
| 5 |
from _pytest.pytester import Pytester |
| 6 |
import pytest |
| 7 |
from pytest import PytestDeprecationWarning |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS)) |
| 11 |
@pytest.mark.filterwarnings("default") |
| 12 |
def test_external_plugins_integrated(pytester: Pytester, plugin) -> None: |
| 13 |
pytester.syspathinsert() |
| 14 |
pytester.makepyfile(**{plugin: ""}) |
| 15 |
|
| 16 |
with pytest.warns(pytest.PytestConfigWarning): |
| 17 |
pytester.parseconfig("-p", plugin) |
| 18 |
|
| 19 |
|
| 20 |
def test_hookspec_via_function_attributes_are_deprecated(): |
| 21 |
from _pytest.config import PytestPluginManager |
| 22 |
|
| 23 |
pm = PytestPluginManager() |
| 24 |
|
| 25 |
class DeprecatedHookMarkerSpec: |
| 26 |
def pytest_bad_hook(self): |
| 27 |
pass |
| 28 |
|
| 29 |
pytest_bad_hook.historic = False # type: ignore[attr-defined] |
| 30 |
|
| 31 |
with pytest.warns( |
| 32 |
PytestDeprecationWarning, |
| 33 |
match=r"Please use the pytest\.hookspec\(historic=False\) decorator", |
| 34 |
) as recorder: |
| 35 |
pm.add_hookspecs(DeprecatedHookMarkerSpec) |
| 36 |
(record,) = recorder |
| 37 |
assert ( |
| 38 |
record.lineno |
| 39 |
== DeprecatedHookMarkerSpec.pytest_bad_hook.__code__.co_firstlineno |
| 40 |
) |
| 41 |
assert record.filename == __file__ |
| 42 |
|
| 43 |
|
| 44 |
def test_hookimpl_via_function_attributes_are_deprecated(): |
| 45 |
from _pytest.config import PytestPluginManager |
| 46 |
|
| 47 |
pm = PytestPluginManager() |
| 48 |
|
| 49 |
class DeprecatedMarkImplPlugin: |
| 50 |
def pytest_runtest_call(self): |
| 51 |
pass |
| 52 |
|
| 53 |
pytest_runtest_call.tryfirst = True # type: ignore[attr-defined] |
| 54 |
|
| 55 |
with pytest.warns( |
| 56 |
PytestDeprecationWarning, |
| 57 |
match=r"Please use the pytest.hookimpl\(tryfirst=True\)", |
| 58 |
) as recorder: |
| 59 |
pm.register(DeprecatedMarkImplPlugin()) |
| 60 |
(record,) = recorder |
| 61 |
assert ( |
| 62 |
record.lineno |
| 63 |
== DeprecatedMarkImplPlugin.pytest_runtest_call.__code__.co_firstlineno |
| 64 |
) |
| 65 |
assert record.filename == __file__ |
| 66 |
|
| 67 |
|
| 68 |
def test_yield_fixture_is_deprecated() -> None: |
| 69 |
with pytest.warns(DeprecationWarning, match=r"yield_fixture is deprecated"): |
| 70 |
|
| 71 |
@pytest.yield_fixture # type: ignore[deprecated] |
| 72 |
def fix(): |
| 73 |
assert False |
| 74 |
|
| 75 |
|
| 76 |
def test_private_is_deprecated() -> None: |
| 77 |
class PrivateInit: |
| 78 |
def __init__(self, foo: int, *, _ispytest: bool = False) -> None: |
| 79 |
deprecated.check_ispytest(_ispytest) |
| 80 |
|
| 81 |
with pytest.warns( |
| 82 |
pytest.PytestDeprecationWarning, match="private pytest class or function" |
| 83 |
): |
| 84 |
PrivateInit(10) |
| 85 |
|
| 86 |
# Doesn't warn. |
| 87 |
PrivateInit(10, _ispytest=True) |
| 88 |
|
| 89 |
|
| 90 |
def test_class_scope_instance_method_is_deprecated(pytester: Pytester) -> None: |
| 91 |
pytester.makepyfile( |
| 92 |
""" |
| 93 |
import pytest |
| 94 |
|
| 95 |
class TestClass: |
| 96 |
@pytest.fixture(scope="class") |
| 97 |
def fix(self): |
| 98 |
self.attr = True |
| 99 |
|
| 100 |
def test_foo(self, fix): |
| 101 |
pass |
| 102 |
""" |
| 103 |
) |
| 104 |
result = pytester.runpytest("-Werror::pytest.PytestRemovedIn10Warning") |
| 105 |
result.assert_outcomes(errors=1) |
| 106 |
result.stdout.fnmatch_lines( |
| 107 |
["*PytestRemovedIn10Warning: Class-scoped fixture defined as instance method*"] |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
def test_class_scope_classmethod_fixture_not_deprecated(pytester: Pytester) -> None: |
| 112 |
"""A class-scoped fixture defined as @classmethod does NOT warn.""" |
| 113 |
pytester.makepyfile( |
| 114 |
""" |
| 115 |
import pytest |
| 116 |
|
| 117 |
class TestClass: |
| 118 |
@pytest.fixture(scope="class") |
| 119 |
@classmethod |
| 120 |
def fix(cls): |
| 121 |
cls.attr = True |
| 122 |
|
| 123 |
def test_foo(self, fix): |
| 124 |
assert type(self).attr is True |
| 125 |
""" |
| 126 |
) |
| 127 |
result = pytester.runpytest("-Werror::pytest.PytestRemovedIn10Warning") |
| 128 |
result.assert_outcomes(passed=1) |
| 129 |
|
| 130 |
|
| 131 |
class TestFixtureNodeidDeprecations: |
| 132 |
"""Tests for deprecated baseid/nodeid string APIs in fixture registration. |
| 133 |
|
| 134 |
AI-generated coverage tests for legacy paths that will be removed in |
| 135 |
pytest 10. These exist solely to maintain patch coverage until the |
| 136 |
deprecated code is deleted. |
| 137 |
|
| 138 |
Legacy paths covered: |
| 139 |
- parsefactories(obj, nodeid_string) deprecation warning |
| 140 |
- parsefactories(obj, None) does NOT warn (standard plugin pattern) |
| 141 |
- parsefactories() with no args raises TypeError |
| 142 |
- _register_fixture(nodeid=string) deprecation warning |
| 143 |
- _nodeid_autousenames population and _getautousenames yield |
| 144 |
- _matchfactories string-based fallback (match + non-match branches) |
| 145 |
""" |
| 146 |
|
| 147 |
def test_parsefactories_nodeid_deprecation(self, pytester: Pytester) -> None: |
| 148 |
"""parsefactories(obj, "path") warns; parsefactories(obj, None) does not.""" |
| 149 |
pytester.makeconftest( |
| 150 |
""" |
| 151 |
import pytest |
| 152 |
import types |
| 153 |
import warnings |
| 154 |
|
| 155 |
def pytest_collection_modifyitems(session, items): |
| 156 |
fm = session._fixturemanager |
| 157 |
|
| 158 |
@pytest.fixture |
| 159 |
def fix_a(): |
| 160 |
return "a" |
| 161 |
|
| 162 |
@pytest.fixture |
| 163 |
def fix_b(): |
| 164 |
return "b" |
| 165 |
|
| 166 |
mod_with_path = types.ModuleType("plugin_path") |
| 167 |
mod_with_path.fix_a = fix_a |
| 168 |
mod_none = types.ModuleType("plugin_none") |
| 169 |
mod_none.fix_b = fix_b |
| 170 |
|
| 171 |
with warnings.catch_warnings(record=True) as w: |
| 172 |
warnings.simplefilter("always") |
| 173 |
fm.parsefactories(mod_with_path, "some/nodeid") |
| 174 |
fm.parsefactories(mod_none, None) |
| 175 |
|
| 176 |
nodeid_warns = [x for x in w if "parsefactories" in str(x.message)] |
| 177 |
assert len(nodeid_warns) == 2, f"Expected 2 warning, got: {w}" |
| 178 |
""" |
| 179 |
) |
| 180 |
pytester.makepyfile( |
| 181 |
""" |
| 182 |
def test_global_fix(fix_b): |
| 183 |
assert fix_b == "b" |
| 184 |
""" |
| 185 |
) |
| 186 |
result = pytester.runpytest("-W", "default::pytest.PytestRemovedIn10Warning") |
| 187 |
result.assert_outcomes(passed=1) |
| 188 |
|
| 189 |
def test_parsefactories_no_args_raises_typeerror(self, pytester: Pytester) -> None: |
| 190 |
"""parsefactories() with no holder and no node_or_obj raises TypeError.""" |
| 191 |
pytester.makeconftest( |
| 192 |
""" |
| 193 |
import pytest |
| 194 |
|
| 195 |
def pytest_collection_modifyitems(session, items): |
| 196 |
fm = session._fixturemanager |
| 197 |
with pytest.raises(TypeError, match="requires holder or node_or_obj"): |
| 198 |
fm.parsefactories() |
| 199 |
""" |
| 200 |
) |
| 201 |
pytester.makepyfile("def test_pass(): pass") |
| 202 |
result = pytester.runpytest() |
| 203 |
result.assert_outcomes(passed=1) |
| 204 |
|
| 205 |
def test_register_fixture_nodeid_and_autouse_legacy( |
| 206 |
self, pytester: Pytester |
| 207 |
) -> None: |
| 208 |
"""_register_fixture(nodeid=string) warns and autouse populates/yields. |
| 209 |
|
| 210 |
Covers end-to-end: |
| 211 |
- Deprecation warning on _register_fixture(nodeid=...) |
| 212 |
- _nodeid_autousenames populated for autouse + non-empty nodeid |
| 213 |
- _getautousenames yields from nodeid_basenames at lookup time |
| 214 |
""" |
| 215 |
pytester.makeconftest( |
| 216 |
""" |
| 217 |
import pytest |
| 218 |
import warnings |
| 219 |
|
| 220 |
_done = False |
| 221 |
|
| 222 |
def pytest_collectstart(collector): |
| 223 |
global _done |
| 224 |
if _done or not hasattr(collector.session, "_fixturemanager"): |
| 225 |
return |
| 226 |
if collector.nodeid == "": |
| 227 |
return |
| 228 |
_done = True |
| 229 |
fm = collector.session._fixturemanager |
| 230 |
with warnings.catch_warnings(record=True) as w: |
| 231 |
warnings.simplefilter("always") |
| 232 |
fm._register_fixture( |
| 233 |
name="legacy_autouse", |
| 234 |
func=lambda: None, |
| 235 |
nodeid=collector.nodeid, |
| 236 |
autouse=True, |
| 237 |
) |
| 238 |
assert any("_register_fixture" in str(x.message) for x in w) |
| 239 |
assert "legacy_autouse" in fm._nodeid_autousenames[collector.nodeid] |
| 240 |
""" |
| 241 |
) |
| 242 |
pytester.makepyfile( |
| 243 |
""" |
| 244 |
def test_autouse_yielded(request): |
| 245 |
assert "legacy_autouse" in request.fixturenames |
| 246 |
""" |
| 247 |
) |
| 248 |
result = pytester.runpytest("-W", "default::pytest.PytestRemovedIn10Warning") |
| 249 |
result.assert_outcomes(passed=1) |
| 250 |
|
| 251 |
def test_matchfactories_string_fallback(self, pytester: Pytester) -> None: |
| 252 |
"""_matchfactories uses baseid string matching for legacy fixtures. |
| 253 |
|
| 254 |
Exercises both branches: |
| 255 |
- baseid="" matches all nodes (global fixture) |
| 256 |
- baseid="nonexistent/path" matches nothing (scoped fixture invisible) |
| 257 |
""" |
| 258 |
pytester.makeconftest( |
| 259 |
""" |
| 260 |
import pytest |
| 261 |
import warnings |
| 262 |
|
| 263 |
def pytest_collection_modifyitems(session, items): |
| 264 |
fm = session._fixturemanager |
| 265 |
with warnings.catch_warnings(): |
| 266 |
warnings.simplefilter("ignore") |
| 267 |
fm._register_fixture( |
| 268 |
name="global_legacy", func=lambda: "ok", nodeid="" |
| 269 |
) |
| 270 |
fm._register_fixture( |
| 271 |
name="scoped_legacy", func=lambda: "nope", |
| 272 |
nodeid="nonexistent/path", |
| 273 |
) |
| 274 |
""" |
| 275 |
) |
| 276 |
pytester.makepyfile( |
| 277 |
""" |
| 278 |
def test_global_visible(global_legacy): |
| 279 |
assert global_legacy == "ok" |
| 280 |
|
| 281 |
def test_scoped_invisible(request): |
| 282 |
defs = request.session._fixturemanager.getfixturedefs( |
| 283 |
"scoped_legacy", request._pyfuncitem |
| 284 |
) |
| 285 |
assert defs == () |
| 286 |
""" |
| 287 |
) |
| 288 |
result = pytester.runpytest("-W", "ignore::pytest.PytestRemovedIn10Warning") |
| 289 |
result.assert_outcomes(passed=2) |
| 290 |
|
| 291 |
def test_fixturedef_has_location_deprecated(self, pytester: Pytester) -> None: |
| 292 |
"""Accessing FixtureDef.has_location warns.""" |
| 293 |
pytester.makepyfile( |
| 294 |
""" |
| 295 |
import pytest |
| 296 |
|
| 297 |
@pytest.fixture |
| 298 |
def fix(): |
| 299 |
return 1 |
| 300 |
|
| 301 |
def test_it(request): |
| 302 |
fixturedef = request._fixturemanager.getfixturedefs( |
| 303 |
"fix", request._pyfuncitem |
| 304 |
)[0] |
| 305 |
with pytest.warns( |
| 306 |
pytest.PytestRemovedIn10Warning, match="has_location" |
| 307 |
): |
| 308 |
assert fixturedef.has_location is True |
| 309 |
""" |
| 310 |
) |
| 311 |
result = pytester.runpytest() |
| 312 |
result.assert_outcomes(passed=1) |