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:
Bruno Oliveira 2017-03-16 21:54:41 -03:00
parent 0baed781fe
commit be5db6fa22
3 changed files with 10 additions and 24 deletions

View File

@ -54,14 +54,12 @@ def deprecated_call(func=None, *args, **kwargs):
def warn_explicit(message, category, *args, **kwargs):
categories.append(category)
old_warn_explicit(message, category, *args, **kwargs)
def warn(message, category=None, *args, **kwargs):
if isinstance(message, Warning):
categories.append(message.__class__)
else:
categories.append(category)
old_warn(message, category, *args, **kwargs)
old_warn = warnings.warn
old_warn_explicit = warnings.warn_explicit

View File

@ -1,4 +1,3 @@
import os
from contextlib import contextmanager
import pytest
@ -50,7 +49,7 @@ def catch_warnings_for_item(item):
args = item.config.getoption('pythonwarnings') or []
inifilters = item.config.getini("filterwarnings")
with warnings.catch_warnings(record=True) as log:
warnings.simplefilter('once')
warnings.simplefilter('always')
for arg in args:
warnings._setoption(arg)
@ -59,27 +58,14 @@ def catch_warnings_for_item(item):
yield
for warning in log:
msg = warnings.formatwarning(
warning.message, warning.category,
warning.filename, warning.lineno, warning.line)
item.warn("unused", msg)
for warning in log:
msg = warnings.formatwarning(
warning.message, warning.category,
warning.filename, warning.lineno, warning.line)
item.warn("unused", msg)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item):
def pytest_runtest_protocol(item):
with catch_warnings_for_item(item):
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

View File

@ -145,7 +145,9 @@ class TestDeprecatedCall(object):
pytest.deprecated_call(deprecated_function)
""")
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):