Skip to content

Commit 14de080

Browse files
committed
fix the unit tests, add the proper deprecation warning, and add in a changelog entry
1 parent d742b38 commit 14de080

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

changelog/7255.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Introduced a new hook named `pytest_warning_recorded` to convey information about warnings captured by the internal `pytest` warnings plugin.
2+
3+
This hook is meant to replace `pytest_warning_captured`, which will be removed in a future release.

src/_pytest/deprecated.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,8 @@
8080
"The `-k 'expr:'` syntax to -k is deprecated.\n"
8181
"Please open an issue if you use this and want a replacement."
8282
)
83+
84+
WARNING_CAPTURED_HOOK = PytestDeprecationWarning(
85+
"The pytest_warning_captured is deprecated and will be removed in a future release.\n"
86+
"Please use pytest_warning_recorded instead."
87+
)

src/_pytest/hookspec.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pluggy import HookspecMarker
99

1010
from .deprecated import COLLECT_DIRECTORY_HOOK
11+
from .deprecated import WARNING_CAPTURED_HOOK
1112
from _pytest.compat import TYPE_CHECKING
1213

1314
if TYPE_CHECKING:
@@ -621,12 +622,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
621622
"""
622623

623624

624-
@hookspec(
625-
historic=True,
626-
warn_on_impl=DeprecationWarning(
627-
"pytest_warning_captured is deprecated and will be removed soon"
628-
),
629-
)
625+
@hookspec(historic=True, warn_on_impl=WARNING_CAPTURED_HOOK)
630626
def pytest_warning_captured(warning_message, when, item, location):
631627
"""(**Deprecated**) Process a warning captured by the internal pytest warnings plugin.
632628

testing/test_warnings.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import re
32
import warnings
43

54
import pytest
@@ -276,25 +275,11 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location):
276275
result.stdout.fnmatch_lines(["*1 passed*"])
277276

278277
expected = [
279-
(
280-
"config warning",
281-
"config",
282-
"",
283-
(
284-
r"/tmp/pytest-of-.+/pytest-\d+/test_warning_captured_hook0/conftest.py",
285-
3,
286-
"pytest_configure",
287-
),
288-
),
289-
("collect warning", "collect", "", None),
290-
("setup warning", "runtest", "test_warning_captured_hook.py::test_func", None),
291-
("call warning", "runtest", "test_warning_captured_hook.py::test_func", None),
292-
(
293-
"teardown warning",
294-
"runtest",
295-
"test_warning_captured_hook.py::test_func",
296-
None,
297-
),
278+
("config warning", "config", "",),
279+
("collect warning", "collect", ""),
280+
("setup warning", "runtest", "test_warning_captured_hook.py::test_func"),
281+
("call warning", "runtest", "test_warning_captured_hook.py::test_func"),
282+
("teardown warning", "runtest", "test_warning_captured_hook.py::test_func"),
298283
]
299284
for index in range(len(expected)):
300285
collected_result = collected[index]
@@ -304,14 +289,15 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location):
304289
assert collected_result[1] == expected_result[1], str(collected)
305290
assert collected_result[2] == expected_result[2], str(collected)
306291

307-
if expected_result[3] is not None:
308-
assert re.match(expected_result[3][0], collected_result[3][0]), str(
309-
collected
310-
)
311-
assert collected_result[3][1] == expected_result[3][1], str(collected)
312-
assert collected_result[3][2] == expected_result[3][2], str(collected)
292+
# NOTE: collected_result[3] is location, which differs based on the platform you are on
293+
# thus, the best we can do here is assert the types of the paremeters match what we expect
294+
# and not try and preload it in the expected array
295+
if collected_result[3] is not None:
296+
assert type(collected_result[3][0]) is str, str(collected)
297+
assert type(collected_result[3][1]) is int, str(collected)
298+
assert type(collected_result[3][2]) is str, str(collected)
313299
else:
314-
assert expected_result[3] == collected_result[3], str(collected)
300+
assert collected_result[3] is None, str(collected)
315301

316302

317303
@pytest.mark.filterwarnings("always")

0 commit comments

Comments
 (0)