From be5db6fa228e0d310194ecd64ad7e6795263c39f Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 16 Mar 2017 21:54:41 -0300 Subject: [PATCH] Capture warnings around the entire runtestprotocol This is necessary for the warnings plugin to play nice with the recwarn fixture --- _pytest/recwarn.py | 2 -- _pytest/warnings.py | 28 +++++++--------------------- testing/test_recwarn.py | 4 +++- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/_pytest/recwarn.py b/_pytest/recwarn.py index 43f68ed12..319bcb836 100644 --- a/_pytest/recwarn.py +++ b/_pytest/recwarn.py @@ -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 diff --git a/_pytest/warnings.py b/_pytest/warnings.py index 9a91d181c..9b18185d2 100644 --- a/_pytest/warnings.py +++ b/_pytest/warnings.py @@ -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 - diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 1269af431..5d5a68c89 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -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):