From 18b18870a8b33c7ad71ceff6dfd7ce79ff0f1a59 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 24 Jul 2020 15:19:07 -0300 Subject: [PATCH] Do not trigger pytest_warning_captured in pytest 6.0+ Fix #562 --- changelog/562.bugfix.rst | 1 + src/xdist/remote.py | 24 ++++++++++++------------ testing/acceptance_test.py | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 changelog/562.bugfix.rst diff --git a/changelog/562.bugfix.rst b/changelog/562.bugfix.rst new file mode 100644 index 00000000..2c9b5662 --- /dev/null +++ b/changelog/562.bugfix.rst @@ -0,0 +1 @@ +Do not trigger the deprecated ``pytest_warning_captured`` in pytest 6.0+. diff --git a/src/xdist/remote.py b/src/xdist/remote.py index 984dffbd..86ba9cbf 100644 --- a/src/xdist/remote.py +++ b/src/xdist/remote.py @@ -139,18 +139,6 @@ def pytest_logwarning(self, message, code, nodeid, fslocation): fslocation=str(fslocation), ) - # the pytest_warning_captured hook was introduced in pytest 3.8 - if hasattr(_pytest.hookspec, "pytest_warning_captured"): - - def pytest_warning_captured(self, warning_message, when, item): - self.sendevent( - "warning_captured", - warning_message_data=serialize_warning_message(warning_message), - when=when, - # item cannot be serialized and will always be None when used with xdist - item=None, - ) - # the pytest_warning_recorded hook was introduced in pytest 6.0 if hasattr(_pytest.hookspec, "pytest_warning_recorded"): @@ -163,6 +151,18 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location): location=location, ) + # the pytest_warning_captured hook was introduced in pytest 3.8 + elif hasattr(_pytest.hookspec, "pytest_warning_captured"): + + def pytest_warning_captured(self, warning_message, when, item): + self.sendevent( + "warning_captured", + warning_message_data=serialize_warning_message(warning_message), + when=when, + # item cannot be serialized and will always be None when used with xdist + item=None, + ) + def serialize_warning_message(warning_message): if isinstance(warning_message.message, Warning): diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index bd6a2ca1..c635b841 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -820,6 +820,32 @@ def test_func(request): result = testdir.runpytest(n) result.stdout.fnmatch_lines(["*this is a warning*", "*1 passed, 1 warning*"]) + def test_warning_captured_deprecated_in_pytest_6(self, testdir): + """ + Do not trigger the deprecated pytest_warning_captured hook in pytest 6+ (#562) + """ + import _pytest.hookspec + + if not hasattr(_pytest.hookspec, "pytest_warning_recorded"): + pytest.skip("test requires pytest 6.0+") + + testdir.makeconftest( + """ + def pytest_warning_captured(): + assert False, "this hook should not be called in this version" + """ + ) + testdir.makepyfile( + """ + import warnings + def test(): + warnings.warn("custom warning") + """ + ) + result = testdir.runpytest("-n1") + result.stdout.fnmatch_lines(["* 1 passed in *"]) + result.stdout.no_fnmatch_line("*this hook should not be called in this version") + @pytest.mark.parametrize("n", ["-n0", "-n1"]) def test_custom_subclass(self, testdir, n): """Check that warning subclasses that don't honor the args attribute don't break