provide missing location parameter, and add type annotations to the hookspec

This commit is contained in:
Gleb Nikonorov 2020-05-27 00:53:31 -04:00
parent 125b663f20
commit d742b386c3
3 changed files with 51 additions and 10 deletions

View File

@ -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.

View File

@ -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,
)
)

View File

@ -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")