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):
|
||||
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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
@ -67,19 +66,6 @@ def catch_warnings_for_item(item):
|
|||
|
||||
|
||||
@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
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue