Skip to content

Commit

Permalink
Do not trigger pytest_warning_captured in pytest 6.0+
Browse files Browse the repository at this point in the history
Fix #562
  • Loading branch information
nicoddemus committed Jul 24, 2020
1 parent 2fc21fd commit 18b1887
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog/562.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not trigger the deprecated ``pytest_warning_captured`` in pytest 6.0+.
24 changes: 12 additions & 12 deletions src/xdist/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):

Expand All @@ -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):
Expand Down
26 changes: 26 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 18b1887

Please sign in to comment.