Do not trigger pytest_warning_captured in pytest 6.0+

Fix #562
This commit is contained in:
Bruno Oliveira
2020-07-24 15:19:07 -03:00
parent 2fc21fddbb
commit 18b18870a8
3 changed files with 39 additions and 12 deletions

1
changelog/562.bugfix.rst Normal file
View File

@@ -0,0 +1 @@
Do not trigger the deprecated ``pytest_warning_captured`` in pytest 6.0+.

View File

@@ -139,18 +139,6 @@ class WorkerInteractor(object):
fslocation=str(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 # the pytest_warning_recorded hook was introduced in pytest 6.0
if hasattr(_pytest.hookspec, "pytest_warning_recorded"): if hasattr(_pytest.hookspec, "pytest_warning_recorded"):
@@ -163,6 +151,18 @@ class WorkerInteractor(object):
location=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): def serialize_warning_message(warning_message):
if isinstance(warning_message.message, Warning): if isinstance(warning_message.message, Warning):

View File

@@ -820,6 +820,32 @@ class TestWarnings:
result = testdir.runpytest(n) result = testdir.runpytest(n)
result.stdout.fnmatch_lines(["*this is a warning*", "*1 passed, 1 warning*"]) 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"]) @pytest.mark.parametrize("n", ["-n0", "-n1"])
def test_custom_subclass(self, testdir, n): def test_custom_subclass(self, testdir, n):
"""Check that warning subclasses that don't honor the args attribute don't break """Check that warning subclasses that don't honor the args attribute don't break