Skip to content

Commit 2c0575d

Browse files
TomAugspurgerjreback
authored andcommitted
TST: assert_produces_warning works with filterwarnings (#25721)
1 parent 15a6534 commit 2c0575d

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
@@ -2536,7 +2536,8 @@ def exception_matches(self, exc_type, exc_value, trace_back):
25362536

25372537
@contextmanager
25382538
def assert_produces_warning(expected_warning=Warning, filter_level="always",
2539-
clear=None, check_stacklevel=True):
2539+
clear=None, check_stacklevel=True,
2540+
raise_on_extra_warnings=True):
25402541
"""
25412542
Context manager for running code expected to either raise a specific
25422543
warning, or not raise any warnings. Verifies that the code raises the
@@ -2549,7 +2550,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
25492550
The type of Exception raised. ``exception.Warning`` is the base
25502551
class for all warnings. To check that no warning is returned,
25512552
specify ``False`` or ``None``.
2552-
filter_level : str, default "always"
2553+
filter_level : str or None, default "always"
25532554
Specifies whether warnings are ignored, displayed, or turned
25542555
into errors.
25552556
Valid values are:
@@ -2573,6 +2574,9 @@ class for all warnings. To check that no warning is returned,
25732574
If True, displays the line that called the function containing
25742575
the warning to show were the function is called. Otherwise, the
25752576
line that implements the function is displayed.
2577+
raise_on_extra_warnings : bool, default True
2578+
Whether extra warnings not of the type `expected_warning` should
2579+
cause the test to fail.
25762580
25772581
Examples
25782582
--------
@@ -2641,8 +2645,10 @@ class for all warnings. To check that no warning is returned,
26412645
msg = "Did not see expected warning of class {name!r}.".format(
26422646
name=expected_warning.__name__)
26432647
assert saw_warning, msg
2644-
assert not extra_warnings, ("Caused unexpected warning(s): {extra!r}."
2645-
).format(extra=extra_warnings)
2648+
if raise_on_extra_warnings and extra_warnings:
2649+
raise AssertionError(
2650+
"Caused unexpected warning(s): {!r}.".format(extra_warnings)
2651+
)
26462652

26472653

26482654
class RNGContext(object):

0 commit comments

Comments
 (0)