Skip to content

Commit 0333143

Browse files
TST: Add unmatched warning messages to assert_produces_warning() error (#42181)
* TST: Add unmatched warning messages to assert_produces_warning() error * remove logic from test and split into 3 * make test names more explicit and reformat * reformat unmatched error message * split unmatched warning into 4 lines in test
1 parent 844b388 commit 0333143

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

pandas/_testing/_warnings.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def _assert_caught_expected_warning(
106106
"""Assert that there was the expected warning among the caught warnings."""
107107
saw_warning = False
108108
matched_message = False
109+
unmatched_messages = []
109110

110111
for actual_warning in caught_warnings:
111112
if issubclass(actual_warning.category, expected_warning):
@@ -116,8 +117,11 @@ def _assert_caught_expected_warning(
116117
):
117118
_assert_raised_with_correct_stacklevel(actual_warning)
118119

119-
if match is not None and re.search(match, str(actual_warning.message)):
120-
matched_message = True
120+
if match is not None:
121+
if re.search(match, str(actual_warning.message)):
122+
matched_message = True
123+
else:
124+
unmatched_messages.append(actual_warning.message)
121125

122126
if not saw_warning:
123127
raise AssertionError(
@@ -128,7 +132,8 @@ def _assert_caught_expected_warning(
128132
if match and not matched_message:
129133
raise AssertionError(
130134
f"Did not see warning {repr(expected_warning.__name__)} "
131-
f"matching {match}"
135+
f"matching '{match}'. The emitted warning messages are "
136+
f"{unmatched_messages}"
132137
)
133138

134139

pandas/tests/util/test_assert_produces_warning.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,49 @@ def test_catch_warning_category_and_match(category, message, match):
9494
warnings.warn(message, category)
9595

9696

97-
@pytest.mark.parametrize(
98-
"message, match",
99-
[
100-
("Warning message", "Not this message"),
101-
("Warning message", "warning"),
102-
("Warning message", r"\d+"),
103-
],
104-
)
105-
def test_fail_to_match(category, message, match):
106-
msg = f"Did not see warning {repr(category.__name__)} matching"
107-
with pytest.raises(AssertionError, match=msg):
97+
def test_fail_to_match_runtime_warning():
98+
category = RuntimeWarning
99+
match = "Did not see this warning"
100+
unmatched = (
101+
r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. "
102+
r"The emitted warning messages are "
103+
r"\[RuntimeWarning\('This is not a match.'\), "
104+
r"RuntimeWarning\('Another unmatched warning.'\)\]"
105+
)
106+
with pytest.raises(AssertionError, match=unmatched):
107+
with tm.assert_produces_warning(category, match=match):
108+
warnings.warn("This is not a match.", category)
109+
warnings.warn("Another unmatched warning.", category)
110+
111+
112+
def test_fail_to_match_future_warning():
113+
category = FutureWarning
114+
match = "Warning"
115+
unmatched = (
116+
r"Did not see warning 'FutureWarning' matching 'Warning'. "
117+
r"The emitted warning messages are "
118+
r"\[FutureWarning\('This is not a match.'\), "
119+
r"FutureWarning\('Another unmatched warning.'\)\]"
120+
)
121+
with pytest.raises(AssertionError, match=unmatched):
122+
with tm.assert_produces_warning(category, match=match):
123+
warnings.warn("This is not a match.", category)
124+
warnings.warn("Another unmatched warning.", category)
125+
126+
127+
def test_fail_to_match_resource_warning():
128+
category = ResourceWarning
129+
match = r"\d+"
130+
unmatched = (
131+
r"Did not see warning 'ResourceWarning' matching '\\d\+'. "
132+
r"The emitted warning messages are "
133+
r"\[ResourceWarning\('This is not a match.'\), "
134+
r"ResourceWarning\('Another unmatched warning.'\)\]"
135+
)
136+
with pytest.raises(AssertionError, match=unmatched):
108137
with tm.assert_produces_warning(category, match=match):
109-
warnings.warn(message, category)
138+
warnings.warn("This is not a match.", category)
139+
warnings.warn("Another unmatched warning.", category)
110140

111141

112142
def test_fail_to_catch_actual_warning(pair_different_warnings):

0 commit comments

Comments
 (0)