Skip to content

Commit d6b2cdc

Browse files
committed
Squashed commit of the following:
commit 3e01ab4 Author: Tom Augspurger <[email protected]> Date: Thu Mar 14 15:26:27 2019 -0500 extra warnings option commit ff66c6b Author: Tom Augspurger <[email protected]> Date: Thu Mar 14 07:49:15 2019 -0500 remove message commit a55659c Author: Tom Augspurger <[email protected]> Date: Thu Mar 14 06:55:49 2019 -0500 handle no warning commit c05306d Author: Tom Augspurger <[email protected]> Date: Thu Mar 14 06:19:30 2019 -0500 take 2 commit f0ad708 Author: Tom Augspurger <[email protected]> Date: Wed Mar 13 21:47:05 2019 -0500 TST: assert_produces_warning works with filterwarnings Previously, assert_produces_warning did not play well with pytest's filterwarnings. ``` ===================================================================================== FAILURES ====================================================================================== ____________________________________________________________________ test_assert_produces_warning_honors_filter _____________________________________________________________________ @pytest.mark.filterwarnings('ignore:f1:FutureWarning') def test_assert_produces_warning_honors_filter(): with tm.assert_produces_warning(RuntimeWarning): > f() pandas/tests/util/test_assert_produces_warning.py:17: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <contextlib._GeneratorContextManager object at 0x11dd61128>, type = None, value = None, traceback = None def __exit__(self, type, value, traceback): if type is None: try: > next(self.gen) E AssertionError: Caused unexpected warning(s): [('FutureWarning', FutureWarning('f1'), '/Users/taugspurger/sandbox/pandas/pandas/tests/util/test_assert_produces_warni ng.py', 10)]. /usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py:119: AssertionError ============================================================================= 1 failed in 0.20 seconds ==============================================================================``` Internally, `assert_produces_warning` sets a new `filter`, which overrides any filters set by pytest. We allow for `filter_level=None` to not set a filter.
1 parent 02cb410 commit d6b2cdc

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import warnings
2+
3+
import pytest
4+
5+
import pandas.util.testing as tm
6+
7+
8+
def f():
9+
warnings.warn('f1', FutureWarning)
10+
warnings.warn('f2', RuntimeWarning)
11+
12+
13+
@pytest.mark.filterwarnings('ignore:f1:FutureWarning')
14+
def test_assert_produces_warning_honors_filter():
15+
# raise by default
16+
with pytest.raises(AssertionError):
17+
with tm.assert_produces_warning(RuntimeWarning):
18+
f()
19+
20+
with tm.assert_produces_warning(RuntimeWarning,
21+
raise_on_extra_warnings=False):
22+
f()

pandas/util/testing.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,8 @@ def exception_matches(self, exc_type, exc_value, trace_back):
25712571

25722572
@contextmanager
25732573
def assert_produces_warning(expected_warning=Warning, filter_level="always",
2574-
clear=None, check_stacklevel=True):
2574+
clear=None, check_stacklevel=True,
2575+
raise_on_extra_warnings=True):
25752576
"""
25762577
Context manager for running code expected to either raise a specific
25772578
warning, or not raise any warnings. Verifies that the code raises the
@@ -2584,7 +2585,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
25842585
The type of Exception raised. ``exception.Warning`` is the base
25852586
class for all warnings. To check that no warning is returned,
25862587
specify ``False`` or ``None``.
2587-
filter_level : str, default "always"
2588+
filter_level : str or None, default "always"
25882589
Specifies whether warnings are ignored, displayed, or turned
25892590
into errors.
25902591
Valid values are:
@@ -2608,6 +2609,9 @@ class for all warnings. To check that no warning is returned,
26082609
If True, displays the line that called the function containing
26092610
the warning to show were the function is called. Otherwise, the
26102611
line that implements the function is displayed.
2612+
raise_on_extra_warnings : bool, default True
2613+
Whether extra warnings not of the type `expected_warning` should
2614+
cause the test to fail.
26112615
26122616
Examples
26132617
--------
@@ -2676,8 +2680,10 @@ class for all warnings. To check that no warning is returned,
26762680
msg = "Did not see expected warning of class {name!r}.".format(
26772681
name=expected_warning.__name__)
26782682
assert saw_warning, msg
2679-
assert not extra_warnings, ("Caused unexpected warning(s): {extra!r}."
2680-
).format(extra=extra_warnings)
2683+
if raise_on_extra_warnings and extra_warnings:
2684+
raise AssertionError(
2685+
"Caused unexpected warning(s): {!r}.".format(extra_warnings)
2686+
)
26812687

26822688

26832689
class RNGContext(object):

0 commit comments

Comments
 (0)