Skip to content

Commit

Permalink
fix the unit tests, add the proper deprecation warning, and add in a …
Browse files Browse the repository at this point in the history
…changelog entry
  • Loading branch information
gnikonorov committed May 28, 2020
1 parent d742b38 commit 14de080
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
3 changes: 3 additions & 0 deletions changelog/7255.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Introduced a new hook named `pytest_warning_recorded` to convey information about warnings captured by the internal `pytest` warnings plugin.

This hook is meant to replace `pytest_warning_captured`, which will be removed in a future release.
5 changes: 5 additions & 0 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@
"The `-k 'expr:'` syntax to -k is deprecated.\n"
"Please open an issue if you use this and want a replacement."
)

WARNING_CAPTURED_HOOK = PytestDeprecationWarning(
"The pytest_warning_captured is deprecated and will be removed in a future release.\n"
"Please use pytest_warning_recorded instead."
)
8 changes: 2 additions & 6 deletions src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pluggy import HookspecMarker

from .deprecated import COLLECT_DIRECTORY_HOOK
from .deprecated import WARNING_CAPTURED_HOOK
from _pytest.compat import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -621,12 +622,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
"""


@hookspec(
historic=True,
warn_on_impl=DeprecationWarning(
"pytest_warning_captured is deprecated and will be removed soon"
),
)
@hookspec(historic=True, warn_on_impl=WARNING_CAPTURED_HOOK)
def pytest_warning_captured(warning_message, when, item, location):
"""(**Deprecated**) Process a warning captured by the internal pytest warnings plugin.
Expand Down
40 changes: 13 additions & 27 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
import warnings

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

expected = [
(
"config warning",
"config",
"",
(
r"/tmp/pytest-of-.+/pytest-\d+/test_warning_captured_hook0/conftest.py",
3,
"pytest_configure",
),
),
("collect warning", "collect", "", None),
("setup warning", "runtest", "test_warning_captured_hook.py::test_func", None),
("call warning", "runtest", "test_warning_captured_hook.py::test_func", None),
(
"teardown warning",
"runtest",
"test_warning_captured_hook.py::test_func",
None,
),
("config warning", "config", "",),
("collect warning", "collect", ""),
("setup warning", "runtest", "test_warning_captured_hook.py::test_func"),
("call warning", "runtest", "test_warning_captured_hook.py::test_func"),
("teardown warning", "runtest", "test_warning_captured_hook.py::test_func"),
]
for index in range(len(expected)):
collected_result = collected[index]
Expand All @@ -304,14 +289,15 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location):
assert collected_result[1] == expected_result[1], str(collected)
assert collected_result[2] == expected_result[2], str(collected)

if expected_result[3] is not None:
assert re.match(expected_result[3][0], collected_result[3][0]), str(
collected
)
assert collected_result[3][1] == expected_result[3][1], str(collected)
assert collected_result[3][2] == expected_result[3][2], str(collected)
# NOTE: collected_result[3] is location, which differs based on the platform you are on
# thus, the best we can do here is assert the types of the paremeters match what we expect
# and not try and preload it in the expected array
if collected_result[3] is not None:
assert type(collected_result[3][0]) is str, str(collected)
assert type(collected_result[3][1]) is int, str(collected)
assert type(collected_result[3][2]) is str, str(collected)
else:
assert expected_result[3] == collected_result[3], str(collected)
assert collected_result[3] is None, str(collected)


@pytest.mark.filterwarnings("always")
Expand Down

0 comments on commit 14de080

Please sign in to comment.