From d742b386c373aeb7ab24e2f5013a537decb97a3a Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Wed, 27 May 2020 00:53:31 -0400 Subject: [PATCH] provide missing location parameter, and add type annotations to the hookspec --- src/_pytest/hookspec.py | 8 ++++++- src/_pytest/warnings.py | 7 +++++- testing/test_warnings.py | 46 +++++++++++++++++++++++++++++++++------- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index e2810ec78..bc8a67ea3 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -11,6 +11,7 @@ from .deprecated import COLLECT_DIRECTORY_HOOK from _pytest.compat import TYPE_CHECKING if TYPE_CHECKING: + import warnings from _pytest.config import Config from _pytest.main import Session from _pytest.reports import BaseReport @@ -653,7 +654,12 @@ def pytest_warning_captured(warning_message, when, item, location): @hookspec(historic=True) -def pytest_warning_recorded(warning_message, when, nodeid, location): +def pytest_warning_recorded( + warning_message: "warnings.WarningMessage", + when: str, + nodeid: str, + location: Tuple[str, int, str], +): """ Process a warning captured by the internal pytest warnings plugin. diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 3ae3c8639..8828a53d6 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -115,7 +115,12 @@ def catch_warnings_for_item(config, ihook, when, item): kwargs=dict(warning_message=warning_message, when=when, item=item) ) ihook.pytest_warning_recorded.call_historic( - kwargs=dict(warning_message=warning_message, nodeid=nodeid, when=when) + kwargs=dict( + warning_message=warning_message, + nodeid=nodeid, + when=when, + location=None, + ) ) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 6cfdfa6bb..8b7ef3296 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -1,4 +1,5 @@ import os +import re import warnings import pytest @@ -268,20 +269,49 @@ def test_warning_captured_hook(testdir): collected = [] class WarningCollector: - def pytest_warning_recorded(self, warning_message, when, nodeid): - collected.append((str(warning_message.message), when, nodeid)) + def pytest_warning_recorded(self, warning_message, when, nodeid, location): + collected.append((str(warning_message.message), when, nodeid, location)) result = testdir.runpytest(plugins=[WarningCollector()]) result.stdout.fnmatch_lines(["*1 passed*"]) expected = [ - ("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"), + ( + "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, + ), ] - assert collected == expected, str(collected) + for index in range(len(expected)): + collected_result = collected[index] + expected_result = expected[index] + + assert collected_result[0] == expected_result[0], str(collected) + 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) + else: + assert expected_result[3] == collected_result[3], str(collected) @pytest.mark.filterwarnings("always")