Skip to content

TST: Add unmatched warning messages to assert_produces_warning() error #42181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pandas/_testing/_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def _assert_caught_expected_warning(
"""Assert that there was the expected warning among the caught warnings."""
saw_warning = False
matched_message = False
unmatched_messages = []

for actual_warning in caught_warnings:
if issubclass(actual_warning.category, expected_warning):
Expand All @@ -116,8 +117,11 @@ def _assert_caught_expected_warning(
):
_assert_raised_with_correct_stacklevel(actual_warning)

if match is not None and re.search(match, str(actual_warning.message)):
matched_message = True
if match is not None:
if re.search(match, str(actual_warning.message)):
matched_message = True
else:
unmatched_messages.append(actual_warning.message)

if not saw_warning:
raise AssertionError(
Expand All @@ -128,7 +132,8 @@ def _assert_caught_expected_warning(
if match and not matched_message:
raise AssertionError(
f"Did not see warning {repr(expected_warning.__name__)} "
f"matching {match}"
f"matching '{match}'. The emitted warning messages are "
f"{unmatched_messages}"
)


Expand Down
19 changes: 11 additions & 8 deletions pandas/tests/util/test_assert_produces_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,21 @@ def test_catch_warning_category_and_match(category, message, match):


@pytest.mark.parametrize(
"message, match",
"match",
[
("Warning message", "Not this message"),
("Warning message", "warning"),
("Warning message", r"\d+"),
("Not this message"),
("Warning"),
(r"\d+"),
],
)
def test_fail_to_match(category, message, match):
msg = f"Did not see warning {repr(category.__name__)} matching"
with pytest.raises(AssertionError, match=msg):
def test_fail_to_match(category, match):
msg1 = "This is not a match."
msg2 = "Another unmatched warning."
unmatched = rf"{category.__name__}\('{msg1}'\), {category.__name__}\('{msg2}'\)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make three distinct tests here, and match the full unmatched error message each time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! Yes, I can do that. I think I see the reasoning for the original three test cases that I could preserve.

with pytest.raises(AssertionError, match=unmatched):
with tm.assert_produces_warning(category, match=match):
warnings.warn(message, category)
warnings.warn(msg1, category)
warnings.warn(msg2, category)


def test_fail_to_catch_actual_warning(pair_different_warnings):
Expand Down