Capture warnings around the entire runtestprotocol
This is necessary for the warnings plugin to play nice with the recwarn fixture
This commit is contained in:
parent
0baed781fe
commit
be5db6fa22
|
@ -54,14 +54,12 @@ def deprecated_call(func=None, *args, **kwargs):
|
||||||
|
|
||||||
def warn_explicit(message, category, *args, **kwargs):
|
def warn_explicit(message, category, *args, **kwargs):
|
||||||
categories.append(category)
|
categories.append(category)
|
||||||
old_warn_explicit(message, category, *args, **kwargs)
|
|
||||||
|
|
||||||
def warn(message, category=None, *args, **kwargs):
|
def warn(message, category=None, *args, **kwargs):
|
||||||
if isinstance(message, Warning):
|
if isinstance(message, Warning):
|
||||||
categories.append(message.__class__)
|
categories.append(message.__class__)
|
||||||
else:
|
else:
|
||||||
categories.append(category)
|
categories.append(category)
|
||||||
old_warn(message, category, *args, **kwargs)
|
|
||||||
|
|
||||||
old_warn = warnings.warn
|
old_warn = warnings.warn
|
||||||
old_warn_explicit = warnings.warn_explicit
|
old_warn_explicit = warnings.warn_explicit
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import os
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -50,7 +49,7 @@ def catch_warnings_for_item(item):
|
||||||
args = item.config.getoption('pythonwarnings') or []
|
args = item.config.getoption('pythonwarnings') or []
|
||||||
inifilters = item.config.getini("filterwarnings")
|
inifilters = item.config.getini("filterwarnings")
|
||||||
with warnings.catch_warnings(record=True) as log:
|
with warnings.catch_warnings(record=True) as log:
|
||||||
warnings.simplefilter('once')
|
warnings.simplefilter('always')
|
||||||
for arg in args:
|
for arg in args:
|
||||||
warnings._setoption(arg)
|
warnings._setoption(arg)
|
||||||
|
|
||||||
|
@ -59,27 +58,14 @@ def catch_warnings_for_item(item):
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
for warning in log:
|
for warning in log:
|
||||||
msg = warnings.formatwarning(
|
msg = warnings.formatwarning(
|
||||||
warning.message, warning.category,
|
warning.message, warning.category,
|
||||||
warning.filename, warning.lineno, warning.line)
|
warning.filename, warning.lineno, warning.line)
|
||||||
item.warn("unused", msg)
|
item.warn("unused", msg)
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
@pytest.hookimpl(hookwrapper=True)
|
||||||
def pytest_runtest_call(item):
|
def pytest_runtest_protocol(item):
|
||||||
with catch_warnings_for_item(item):
|
with catch_warnings_for_item(item):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
|
||||||
def pytest_runtest_setup(item):
|
|
||||||
with catch_warnings_for_item(item):
|
|
||||||
yield
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True)
|
|
||||||
def pytest_runtest_teardown(item):
|
|
||||||
with catch_warnings_for_item(item):
|
|
||||||
yield
|
|
||||||
|
|
||||||
|
|
|
@ -145,7 +145,9 @@ class TestDeprecatedCall(object):
|
||||||
pytest.deprecated_call(deprecated_function)
|
pytest.deprecated_call(deprecated_function)
|
||||||
""")
|
""")
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines('*=== 2 passed in *===')
|
# the 2 tests must pass, but the call to test_one() will generate a warning
|
||||||
|
# in pytest's summary
|
||||||
|
result.stdout.fnmatch_lines('*=== 2 passed, 1 warnings in *===')
|
||||||
|
|
||||||
|
|
||||||
class TestWarns(object):
|
class TestWarns(object):
|
||||||
|
|
Loading…
Reference in New Issue