Skip to content

Commit 78eaa4c

Browse files
authored
Merge pull request #563 from nicoddemus/pytest-warning-captured-562
Do not trigger pytest_warning_captured in pytest 6.0+
2 parents 2fc21fd + 18b1887 commit 78eaa4c

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

changelog/562.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not trigger the deprecated ``pytest_warning_captured`` in pytest 6.0+.

src/xdist/remote.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,6 @@ def pytest_logwarning(self, message, code, nodeid, fslocation):
139139
fslocation=str(fslocation),
140140
)
141141

142-
# the pytest_warning_captured hook was introduced in pytest 3.8
143-
if hasattr(_pytest.hookspec, "pytest_warning_captured"):
144-
145-
def pytest_warning_captured(self, warning_message, when, item):
146-
self.sendevent(
147-
"warning_captured",
148-
warning_message_data=serialize_warning_message(warning_message),
149-
when=when,
150-
# item cannot be serialized and will always be None when used with xdist
151-
item=None,
152-
)
153-
154142
# the pytest_warning_recorded hook was introduced in pytest 6.0
155143
if hasattr(_pytest.hookspec, "pytest_warning_recorded"):
156144

@@ -163,6 +151,18 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location):
163151
location=location,
164152
)
165153

154+
# the pytest_warning_captured hook was introduced in pytest 3.8
155+
elif hasattr(_pytest.hookspec, "pytest_warning_captured"):
156+
157+
def pytest_warning_captured(self, warning_message, when, item):
158+
self.sendevent(
159+
"warning_captured",
160+
warning_message_data=serialize_warning_message(warning_message),
161+
when=when,
162+
# item cannot be serialized and will always be None when used with xdist
163+
item=None,
164+
)
165+
166166

167167
def serialize_warning_message(warning_message):
168168
if isinstance(warning_message.message, Warning):

testing/acceptance_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,32 @@ def test_func(request):
820820
result = testdir.runpytest(n)
821821
result.stdout.fnmatch_lines(["*this is a warning*", "*1 passed, 1 warning*"])
822822

823+
def test_warning_captured_deprecated_in_pytest_6(self, testdir):
824+
"""
825+
Do not trigger the deprecated pytest_warning_captured hook in pytest 6+ (#562)
826+
"""
827+
import _pytest.hookspec
828+
829+
if not hasattr(_pytest.hookspec, "pytest_warning_recorded"):
830+
pytest.skip("test requires pytest 6.0+")
831+
832+
testdir.makeconftest(
833+
"""
834+
def pytest_warning_captured():
835+
assert False, "this hook should not be called in this version"
836+
"""
837+
)
838+
testdir.makepyfile(
839+
"""
840+
import warnings
841+
def test():
842+
warnings.warn("custom warning")
843+
"""
844+
)
845+
result = testdir.runpytest("-n1")
846+
result.stdout.fnmatch_lines(["* 1 passed in *"])
847+
result.stdout.no_fnmatch_line("*this hook should not be called in this version")
848+
823849
@pytest.mark.parametrize("n", ["-n0", "-n1"])
824850
def test_custom_subclass(self, testdir, n):
825851
"""Check that warning subclasses that don't honor the args attribute don't break

0 commit comments

Comments
 (0)