Skip to content

Commit f0ad708

Browse files
committed
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 79205ea commit f0ad708

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
with tm.assert_produces_warning(RuntimeWarning, filter_level=None):
16+
f()

pandas/util/testing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2584,7 +2584,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
25842584
The type of Exception raised. ``exception.Warning`` is the base
25852585
class for all warnings. To check that no warning is returned,
25862586
specify ``False`` or ``None``.
2587-
filter_level : str, default "always"
2587+
filter_level : str or None, default "always"
25882588
Specifies whether warnings are ignored, displayed, or turned
25892589
into errors.
25902590
Valid values are:
@@ -2597,6 +2597,7 @@ class for all warnings. To check that no warning is returned,
25972597
* "module" - print the warning the first time it is generated
25982598
from each module
25992599
* "once" - print the warning the first time it is generated
2600+
* None - do not apply a new filter
26002601
26012602
clear : str, default None
26022603
If not ``None`` then remove any previously raised warnings from
@@ -2646,7 +2647,8 @@ class for all warnings. To check that no warning is returned,
26462647
pass
26472648

26482649
saw_warning = False
2649-
warnings.simplefilter(filter_level)
2650+
if filter_level:
2651+
warnings.simplefilter(filter_level)
26502652
yield w
26512653
extra_warnings = []
26522654

0 commit comments

Comments
 (0)