From 9ee65501813cfc00c3dbe262c08d58377dd05c31 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sun, 24 May 2020 02:12:40 -0400 Subject: [PATCH 1/7] Add in a new hook pytest_warning_recorded for warning capture communication --- doc/en/reference.rst | 1 + src/_pytest/hookspec.py | 33 ++++++++++++++++++++++++++++----- src/_pytest/terminal.py | 7 ++++--- src/_pytest/warnings.py | 17 ++++++++++------- testing/test_warnings.py | 15 +++++++-------- 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 6bc7657c570..13d2484bbf9 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -711,6 +711,7 @@ Session related reporting hooks: .. autofunction:: pytest_fixture_setup .. autofunction:: pytest_fixture_post_finalizer .. autofunction:: pytest_warning_captured +.. autofunction:: pytest_warning_record Central hook for reporting about test execution: diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index b4fab332d43..c983f87f9dc 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -622,8 +622,10 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): @hookspec(historic=True) def pytest_warning_captured(warning_message, when, item, location): - """ - Process a warning captured by the internal pytest warnings plugin. + """(**Deprecated**) Process a warning captured by the internal pytest warnings plugin. + + This hook is considered deprecated and will be removed in a future pytest version. + Use :func:`pytest_warning_record` instead. :param warnings.WarningMessage warning_message: The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains @@ -637,9 +639,6 @@ def pytest_warning_captured(warning_message, when, item, location): * ``"runtest"``: during test execution. :param pytest.Item|None item: - **DEPRECATED**: This parameter is incompatible with ``pytest-xdist``, and will always receive ``None`` - in a future release. - The item being executed if ``when`` is ``"runtest"``, otherwise ``None``. :param tuple location: @@ -648,6 +647,30 @@ def pytest_warning_captured(warning_message, when, item, location): """ +@hookspec(historic=True) +def pytest_warning_record(warning_message, when, nodeid, location): + """ + Process a warning captured by the internal pytest warnings plugin. + + :param warnings.WarningMessage warning_message: + The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains + the same attributes as the parameters of :py:func:`warnings.showwarning`. + + :param str when: + Indicates when the warning was captured. Possible values: + + * ``"config"``: during pytest configuration/initialization stage. + * ``"collect"``: during test collection. + * ``"runtest"``: during test execution. + + :param str nodeid: full id of the item + + :param tuple location: + Holds information about the execution context of the captured warning (filename, linenumber, function). + ``function`` evaluates to when the execution context is at the module level. + """ + + # ------------------------------------------------------------------------- # doctest hooks # ------------------------------------------------------------------------- diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 8ecb5a16b63..2524fb21fe1 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -227,7 +227,7 @@ def pytest_report_teststatus(report: TestReport) -> Tuple[str, str, str]: @attr.s class WarningReport: """ - Simple structure to hold warnings information captured by ``pytest_warning_captured``. + Simple structure to hold warnings information captured by ``pytest_warning_record``. :ivar str message: user friendly message about the warning :ivar str|None nodeid: node id that generated the warning (see ``get_location``). @@ -412,13 +412,14 @@ def pytest_internalerror(self, excrepr): return 1 def pytest_warning_captured(self, warning_message, item): - # from _pytest.nodes import get_fslocation_from_item + pass + + def pytest_warning_record(self, warning_message, nodeid): from _pytest.warnings import warning_record_to_str fslocation = warning_message.filename, warning_message.lineno message = warning_record_to_str(warning_message) - nodeid = item.nodeid if item is not None else "" warning_report = WarningReport( fslocation=fslocation, message=message, nodeid=nodeid ) diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 527bb03b001..16d8de8f8ba 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -81,7 +81,7 @@ def catch_warnings_for_item(config, ihook, when, item): ``item`` can be None if we are not in the context of an item execution. - Each warning captured triggers the ``pytest_warning_captured`` hook. + Each warning captured triggers the ``pytest_warning_record`` hook. """ cmdline_filters = config.getoption("pythonwarnings") or [] inifilters = config.getini("filterwarnings") @@ -102,6 +102,7 @@ def catch_warnings_for_item(config, ihook, when, item): for arg in cmdline_filters: warnings.filterwarnings(*_parse_filter(arg, escape=True)) + nodeid = "" if item is None else item.nodeid if item is not None: for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: @@ -110,8 +111,9 @@ def catch_warnings_for_item(config, ihook, when, item): yield for warning_message in log: - ihook.pytest_warning_captured.call_historic( - kwargs=dict(warning_message=warning_message, when=when, item=item) + # raise ValueError(ihook.pytest_warning_record) + ihook.pytest_warning_record.call_historic( + kwargs=dict(warning_message=warning_message, nodeid=nodeid, when=when) ) @@ -166,8 +168,9 @@ def pytest_sessionfinish(session): def _issue_warning_captured(warning, hook, stacklevel): """ This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: - at this point the actual options might not have been set, so we manually trigger the pytest_warning_captured - hook so we can display these warnings in the terminal. This is a hack until we can sort out #2891. + at this point the actual options might not have been set, so we manually trigger the pytest_warning_record hooks + so we can display these warnings in the terminal. + This is a hack until we can sort out #2891. :param warning: the warning instance. :param hook: the hook caller @@ -180,8 +183,8 @@ def _issue_warning_captured(warning, hook, stacklevel): assert records is not None frame = sys._getframe(stacklevel - 1) location = frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name - hook.pytest_warning_captured.call_historic( + hook.pytest_warning_record.call_historic( kwargs=dict( - warning_message=records[0], when="config", item=None, location=location + warning_message=records[0], when="config", nodeid="", location=location ) ) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 51d1286b465..ec75e657141 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -268,9 +268,8 @@ def test_func(fix): collected = [] class WarningCollector: - def pytest_warning_captured(self, warning_message, when, item): - imge_name = item.name if item is not None else "" - collected.append((str(warning_message.message), when, imge_name)) + def pytest_warning_record(self, warning_message, when, nodeid): + collected.append((str(warning_message.message), when, nodeid)) result = testdir.runpytest(plugins=[WarningCollector()]) result.stdout.fnmatch_lines(["*1 passed*"]) @@ -278,11 +277,11 @@ def pytest_warning_captured(self, warning_message, when, item): expected = [ ("config warning", "config", ""), ("collect warning", "collect", ""), - ("setup warning", "runtest", "test_func"), - ("call warning", "runtest", "test_func"), - ("teardown warning", "runtest", "test_func"), + ("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"), ] - assert collected == expected + assert collected == expected, str(collected) @pytest.mark.filterwarnings("always") @@ -649,7 +648,7 @@ class CapturedWarnings: captured = [] @classmethod - def pytest_warning_captured(cls, warning_message, when, item, location): + def pytest_warning_record(cls, warning_message, when, nodeid, location): cls.captured.append((warning_message, location)) testdir.plugins = [CapturedWarnings()] From b02d087dbdd6f8b0a4233314356b2442da7fc6c5 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sun, 24 May 2020 20:26:14 -0400 Subject: [PATCH 2/7] cleanup code pre pr --- src/_pytest/warnings.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 16d8de8f8ba..4aa2321aa10 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -111,7 +111,6 @@ def catch_warnings_for_item(config, ihook, when, item): yield for warning_message in log: - # raise ValueError(ihook.pytest_warning_record) ihook.pytest_warning_record.call_historic( kwargs=dict(warning_message=warning_message, nodeid=nodeid, when=when) ) @@ -168,9 +167,8 @@ def pytest_sessionfinish(session): def _issue_warning_captured(warning, hook, stacklevel): """ This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: - at this point the actual options might not have been set, so we manually trigger the pytest_warning_record hooks - so we can display these warnings in the terminal. - This is a hack until we can sort out #2891. + at this point the actual options might not have been set, so we manually trigger the pytest_warning_record + hook so we can display these warnings in the terminal. This is a hack until we can sort out #2891. :param warning: the warning instance. :param hook: the hook caller From 088d400b2da633c3eaa7e2b9b6edef18f88dd885 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Sun, 24 May 2020 20:43:23 -0400 Subject: [PATCH 3/7] rename pytest_warning_record -> pytest_warning_recorded --- doc/en/reference.rst | 2 +- src/_pytest/hookspec.py | 4 ++-- src/_pytest/terminal.py | 4 ++-- src/_pytest/warnings.py | 8 ++++---- testing/test_warnings.py | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 13d2484bbf9..7348636a2dd 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -711,7 +711,7 @@ Session related reporting hooks: .. autofunction:: pytest_fixture_setup .. autofunction:: pytest_fixture_post_finalizer .. autofunction:: pytest_warning_captured -.. autofunction:: pytest_warning_record +.. autofunction:: pytest_warning_recorded Central hook for reporting about test execution: diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index c983f87f9dc..8ccb89ca550 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -625,7 +625,7 @@ def pytest_warning_captured(warning_message, when, item, location): """(**Deprecated**) Process a warning captured by the internal pytest warnings plugin. This hook is considered deprecated and will be removed in a future pytest version. - Use :func:`pytest_warning_record` instead. + Use :func:`pytest_warning_recorded` instead. :param warnings.WarningMessage warning_message: The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains @@ -648,7 +648,7 @@ def pytest_warning_captured(warning_message, when, item, location): @hookspec(historic=True) -def pytest_warning_record(warning_message, when, nodeid, location): +def pytest_warning_recorded(warning_message, when, nodeid, location): """ Process a warning captured by the internal pytest warnings plugin. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 2524fb21fe1..10fb1f76945 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -227,7 +227,7 @@ def pytest_report_teststatus(report: TestReport) -> Tuple[str, str, str]: @attr.s class WarningReport: """ - Simple structure to hold warnings information captured by ``pytest_warning_record``. + Simple structure to hold warnings information captured by ``pytest_warning_recorded``. :ivar str message: user friendly message about the warning :ivar str|None nodeid: node id that generated the warning (see ``get_location``). @@ -414,7 +414,7 @@ def pytest_internalerror(self, excrepr): def pytest_warning_captured(self, warning_message, item): pass - def pytest_warning_record(self, warning_message, nodeid): + def pytest_warning_recorded(self, warning_message, nodeid): from _pytest.warnings import warning_record_to_str fslocation = warning_message.filename, warning_message.lineno diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 4aa2321aa10..6d383ccddcd 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -81,7 +81,7 @@ def catch_warnings_for_item(config, ihook, when, item): ``item`` can be None if we are not in the context of an item execution. - Each warning captured triggers the ``pytest_warning_record`` hook. + Each warning captured triggers the ``pytest_warning_recorded`` hook. """ cmdline_filters = config.getoption("pythonwarnings") or [] inifilters = config.getini("filterwarnings") @@ -111,7 +111,7 @@ def catch_warnings_for_item(config, ihook, when, item): yield for warning_message in log: - ihook.pytest_warning_record.call_historic( + ihook.pytest_warning_recorded.call_historic( kwargs=dict(warning_message=warning_message, nodeid=nodeid, when=when) ) @@ -167,7 +167,7 @@ def pytest_sessionfinish(session): def _issue_warning_captured(warning, hook, stacklevel): """ This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: - at this point the actual options might not have been set, so we manually trigger the pytest_warning_record + at this point the actual options might not have been set, so we manually trigger the pytest_warning_recorded hook so we can display these warnings in the terminal. This is a hack until we can sort out #2891. :param warning: the warning instance. @@ -181,7 +181,7 @@ def _issue_warning_captured(warning, hook, stacklevel): assert records is not None frame = sys._getframe(stacklevel - 1) location = frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name - hook.pytest_warning_record.call_historic( + hook.pytest_warning_recorded.call_historic( kwargs=dict( warning_message=records[0], when="config", nodeid="", location=location ) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index ec75e657141..6cfdfa6bb81 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -268,7 +268,7 @@ def test_func(fix): collected = [] class WarningCollector: - def pytest_warning_record(self, warning_message, when, nodeid): + def pytest_warning_recorded(self, warning_message, when, nodeid): collected.append((str(warning_message.message), when, nodeid)) result = testdir.runpytest(plugins=[WarningCollector()]) @@ -648,7 +648,7 @@ class CapturedWarnings: captured = [] @classmethod - def pytest_warning_record(cls, warning_message, when, nodeid, location): + def pytest_warning_recorded(cls, warning_message, when, nodeid, location): cls.captured.append((warning_message, location)) testdir.plugins = [CapturedWarnings()] From 125b663f20a599e7e39fce821597a122d62a6460 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Mon, 25 May 2020 11:18:24 -0400 Subject: [PATCH 4/7] Address all feedback, minus the empty sring v None nodeid which is being discussed --- src/_pytest/hookspec.py | 7 ++++++- src/_pytest/terminal.py | 3 --- src/_pytest/warnings.py | 8 ++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index 8ccb89ca550..e2810ec78bc 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -620,7 +620,12 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config): """ -@hookspec(historic=True) +@hookspec( + historic=True, + warn_on_impl=DeprecationWarning( + "pytest_warning_captured is deprecated and will be removed soon" + ), +) def pytest_warning_captured(warning_message, when, item, location): """(**Deprecated**) Process a warning captured by the internal pytest warnings plugin. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 10fb1f76945..d26db2c510b 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -411,9 +411,6 @@ def pytest_internalerror(self, excrepr): self.write_line("INTERNALERROR> " + line) return 1 - def pytest_warning_captured(self, warning_message, item): - pass - def pytest_warning_recorded(self, warning_message, nodeid): from _pytest.warnings import warning_record_to_str diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 6d383ccddcd..3ae3c863929 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -111,6 +111,9 @@ def catch_warnings_for_item(config, ihook, when, item): yield for warning_message in log: + ihook.pytest_warning_captured.call_historic( + 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) ) @@ -181,6 +184,11 @@ def _issue_warning_captured(warning, hook, stacklevel): assert records is not None frame = sys._getframe(stacklevel - 1) location = frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name + hook.pytest_warning_captured.call_historic( + kwargs=dict( + warning_message=records[0], when="config", item=None, location=location + ) + ) hook.pytest_warning_recorded.call_historic( kwargs=dict( warning_message=records[0], when="config", nodeid="", location=location From d742b386c373aeb7ab24e2f5013a537decb97a3a Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Wed, 27 May 2020 00:53:31 -0400 Subject: [PATCH 5/7] 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 e2810ec78bc..bc8a67ea3d2 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -11,6 +11,7 @@ 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 3ae3c863929..8828a53d611 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 6cfdfa6bb81..8b7ef32963a 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_func(fix): 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") From 14de08011b05bffe66df6198ac4ccbac5c8aa7fe Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Wed, 27 May 2020 23:03:07 -0400 Subject: [PATCH 6/7] fix the unit tests, add the proper deprecation warning, and add in a changelog entry --- changelog/7255.feature.rst | 3 +++ src/_pytest/deprecated.py | 5 +++++ src/_pytest/hookspec.py | 8 ++------ testing/test_warnings.py | 40 +++++++++++++------------------------- 4 files changed, 23 insertions(+), 33 deletions(-) create mode 100644 changelog/7255.feature.rst diff --git a/changelog/7255.feature.rst b/changelog/7255.feature.rst new file mode 100644 index 00000000000..4073589b05a --- /dev/null +++ b/changelog/7255.feature.rst @@ -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. diff --git a/src/_pytest/deprecated.py b/src/_pytest/deprecated.py index f981a4a4b9e..1ce4e1e39b2 100644 --- a/src/_pytest/deprecated.py +++ b/src/_pytest/deprecated.py @@ -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." +) diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index bc8a67ea3d2..341f0a250b2 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -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: @@ -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. diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 8b7ef32963a..070ed72c51f 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -1,5 +1,4 @@ import os -import re import warnings import pytest @@ -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] @@ -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") From 2af0d1e221739ebad1d318743d2987f3d45511f8 Mon Sep 17 00:00:00 2001 From: Gleb Nikonorov Date: Thu, 28 May 2020 00:02:28 -0400 Subject: [PATCH 7/7] remove a stray comma in a test tuple --- testing/test_warnings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 070ed72c51f..ea7ab397dfe 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -275,7 +275,7 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location): result.stdout.fnmatch_lines(["*1 passed*"]) expected = [ - ("config warning", "config", "",), + ("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"),