Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/testing/deprecated_test.py

raw
1 # mypy: allow-untyped-defs
2 from pathlib import Path
3 import re
4 import sys
5
6 from _pytest import deprecated
7 from _pytest.compat import legacy_path
8 from _pytest.pytester import Pytester
9 import pytest
10 from pytest import PytestDeprecationWarning
11
12
13 @pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))
14 @pytest.mark.filterwarnings("default")
15 def test_external_plugins_integrated(pytester: Pytester, plugin) -> None:
16 pytester.syspathinsert()
17 pytester.makepyfile(**{plugin: ""})
18
19 with pytest.warns(pytest.PytestConfigWarning):
20 pytester.parseconfig("-p", plugin)
21
22
23 def test_hookspec_via_function_attributes_are_deprecated():
24 from _pytest.config import PytestPluginManager
25
26 pm = PytestPluginManager()
27
28 class DeprecatedHookMarkerSpec:
29 def pytest_bad_hook(self):
30 pass
31
32 pytest_bad_hook.historic = False # type: ignore[attr-defined]
33
34 with pytest.warns(
35 PytestDeprecationWarning,
36 match=r"Please use the pytest\.hookspec\(historic=False\) decorator",
37 ) as recorder:
38 pm.add_hookspecs(DeprecatedHookMarkerSpec)
39 (record,) = recorder
40 assert (
41 record.lineno
42 == DeprecatedHookMarkerSpec.pytest_bad_hook.__code__.co_firstlineno
43 )
44 assert record.filename == __file__
45
46
47 def test_hookimpl_via_function_attributes_are_deprecated():
48 from _pytest.config import PytestPluginManager
49
50 pm = PytestPluginManager()
51
52 class DeprecatedMarkImplPlugin:
53 def pytest_runtest_call(self):
54 pass
55
56 pytest_runtest_call.tryfirst = True # type: ignore[attr-defined]
57
58 with pytest.warns(
59 PytestDeprecationWarning,
60 match=r"Please use the pytest.hookimpl\(tryfirst=True\)",
61 ) as recorder:
62 pm.register(DeprecatedMarkImplPlugin())
63 (record,) = recorder
64 assert (
65 record.lineno
66 == DeprecatedMarkImplPlugin.pytest_runtest_call.__code__.co_firstlineno
67 )
68 assert record.filename == __file__
69
70
71 def test_yield_fixture_is_deprecated() -> None:
72 with pytest.warns(DeprecationWarning, match=r"yield_fixture is deprecated"):
73
74 @pytest.yield_fixture
75 def fix():
76 assert False
77
78
79 def test_private_is_deprecated() -> None:
80 class PrivateInit:
81 def __init__(self, foo: int, *, _ispytest: bool = False) -> None:
82 deprecated.check_ispytest(_ispytest)
83
84 with pytest.warns(
85 pytest.PytestDeprecationWarning, match="private pytest class or function"
86 ):
87 PrivateInit(10)
88
89 # Doesn't warn.
90 PrivateInit(10, _ispytest=True)
91
92
93 @pytest.mark.parametrize("hooktype", ["hook", "ihook"])
94 def test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request):
95 path = legacy_path(tmp_path)
96
97 PATH_WARN_MATCH = r".*path: py\.path\.local\) argument is deprecated, please use \(collection_path: pathlib\.Path.*"
98 if hooktype == "ihook":
99 hooks = request.node.ihook
100 else:
101 hooks = request.config.hook
102
103 with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
104 l1 = sys._getframe().f_lineno
105 hooks.pytest_ignore_collect(
106 config=request.config, path=path, collection_path=tmp_path
107 )
108 l2 = sys._getframe().f_lineno
109
110 (record,) = r
111 assert record.filename == __file__
112 assert l1 < record.lineno < l2
113
114 hooks.pytest_ignore_collect(config=request.config, collection_path=tmp_path)
115
116 # Passing entirely *different* paths is an outright error.
117 with pytest.raises(ValueError, match=r"path.*fspath.*need to be equal"):
118 with pytest.warns(PytestDeprecationWarning, match=PATH_WARN_MATCH) as r:
119 hooks.pytest_ignore_collect(
120 config=request.config, path=path, collection_path=Path("/bla/bla")
121 )
122
123
124 def test_hookimpl_warnings_for_pathlib() -> None:
125 class Plugin:
126 def pytest_ignore_collect(self, path: object) -> None:
127 raise NotImplementedError()
128
129 def pytest_collect_file(self, path: object) -> None:
130 raise NotImplementedError()
131
132 def pytest_pycollect_makemodule(self, path: object) -> None:
133 raise NotImplementedError()
134
135 def pytest_report_header(self, startdir: object) -> str:
136 raise NotImplementedError()
137
138 def pytest_report_collectionfinish(self, startdir: object) -> str:
139 raise NotImplementedError()
140
141 pm = pytest.PytestPluginManager()
142 with pytest.warns(
143 pytest.PytestRemovedIn9Warning,
144 match=r"py\.path\.local.* argument is deprecated",
145 ) as wc:
146 pm.register(Plugin())
147 assert len(wc.list) == 5
148
149
150 def test_node_ctor_fspath_argument_is_deprecated(pytester: Pytester) -> None:
151 mod = pytester.getmodulecol("")
152
153 class MyFile(pytest.File):
154 def collect(self):
155 raise NotImplementedError()
156
157 with pytest.warns(
158 pytest.PytestDeprecationWarning,
159 match=re.escape(
160 "The (fspath: py.path.local) argument to MyFile is deprecated."
161 ),
162 ):
163 MyFile.from_parent(
164 parent=mod.parent,
165 fspath=legacy_path("bla"),
166 )
167
168
169 def test_fixture_disallow_on_marked_functions():
170 """Test that applying @pytest.fixture to a marked function warns (#3364)."""
171 with pytest.warns(
172 pytest.PytestRemovedIn9Warning,
173 match=r"Marks applied to fixtures have no effect",
174 ) as record:
175
176 @pytest.fixture
177 @pytest.mark.parametrize("example", ["hello"])
178 @pytest.mark.usefixtures("tmp_path")
179 def foo():
180 raise NotImplementedError()
181
182 # it's only possible to get one warning here because you're already prevented
183 # from applying @fixture twice
184 # ValueError("fixture is being applied more than once to the same function")
185 assert len(record) == 1
186
187
188 def test_fixture_disallow_marks_on_fixtures():
189 """Test that applying a mark to a fixture warns (#3364)."""
190 with pytest.warns(
191 pytest.PytestRemovedIn9Warning,
192 match=r"Marks applied to fixtures have no effect",
193 ) as record:
194
195 @pytest.mark.parametrize("example", ["hello"])
196 @pytest.mark.usefixtures("tmp_path")
197 @pytest.fixture
198 def foo():
199 raise NotImplementedError()
200
201 assert len(record) == 2 # one for each mark decorator
202 # should point to this file
203 assert all(rec.filename == __file__ for rec in record)
204
205
206 def test_fixture_disallowed_between_marks():
207 """Test that applying a mark to a fixture warns (#3364)."""
208 with pytest.warns(
209 pytest.PytestRemovedIn9Warning,
210 match=r"Marks applied to fixtures have no effect",
211 ) as record:
212
213 @pytest.mark.parametrize("example", ["hello"])
214 @pytest.fixture
215 @pytest.mark.usefixtures("tmp_path")
216 def foo():
217 raise NotImplementedError()
218
219 assert len(record) == 2 # one for each mark decorator