Skip to content

Commit c05306d

Browse files
committed
take 2
1 parent f0ad708 commit c05306d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

pandas/tests/util/test_assert_produces_warning.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
import pandas.util.testing as tm
66

77

8-
def f():
9-
warnings.warn('f1', FutureWarning)
10-
warnings.warn('f2', RuntimeWarning)
8+
def f(a=FutureWarning, b=RuntimeWarning):
9+
warnings.warn('f1', a)
10+
warnings.warn('f2', b)
1111

1212

1313
@pytest.mark.filterwarnings('ignore:f1:FutureWarning')
14+
@pytest.mark.filterwarnings('ignore:f2:RuntimeWarning')
1415
def test_assert_produces_warning_honors_filter():
15-
with tm.assert_produces_warning(RuntimeWarning, filter_level=None):
16+
with tm.assert_produces_warning(RuntimeWarning):
1617
f()
18+
19+
20+
@pytest.mark.filterwarnings('ignore:f1:FutureWarning')
21+
def test_assert_produces_warning_message():
22+
with tm.assert_produces_warning(FutureWarning, message='f2'):
23+
f(FutureWarning, FutureWarning)

pandas/util/testing.py

+6-2
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+
message=''):
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
@@ -2609,6 +2610,9 @@ class for all warnings. To check that no warning is returned,
26092610
If True, displays the line that called the function containing
26102611
the warning to show were the function is called. Otherwise, the
26112612
line that implements the function is displayed.
2613+
message : str, default ''
2614+
Use in the filter with `filter_level` and `expected_warning`
2615+
the control which warnings the filter applies to.
26122616
26132617
Examples
26142618
--------
@@ -2648,7 +2652,7 @@ class for all warnings. To check that no warning is returned,
26482652

26492653
saw_warning = False
26502654
if filter_level:
2651-
warnings.simplefilter(filter_level)
2655+
warnings.filterwarnings(filter_level, message, expected_warning)
26522656
yield w
26532657
extra_warnings = []
26542658

0 commit comments

Comments
 (0)