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 all commits
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
54 changes: 42 additions & 12 deletions pandas/tests/util/test_assert_produces_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,49 @@ def test_catch_warning_category_and_match(category, message, match):
warnings.warn(message, category)


@pytest.mark.parametrize(
"message, match",
[
("Warning message", "Not this message"),
("Warning message", "warning"),
("Warning message", 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_runtime_warning():
category = RuntimeWarning
match = "Did not see this warning"
unmatched = (
r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. "
Copy link
Member

Choose a reason for hiding this comment

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

Would it be more readable if you split the message in lines this way:

    r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. "
    r"The emitted warning messages are "
    r"\[RuntimeWarning\('This is not a match.'\), "
    r"RuntimeWarning\('Another unmatched warning.'\)\]"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think that is better. I'll do that.

Copy link
Member

Choose a reason for hiding this comment

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

Great, not it looks much cleaner here and in other places as well!

r"The emitted warning messages are "
r"\[RuntimeWarning\('This is not a match.'\), "
r"RuntimeWarning\('Another unmatched warning.'\)\]"
)
with pytest.raises(AssertionError, match=unmatched):
with tm.assert_produces_warning(category, match=match):
warnings.warn("This is not a match.", category)
warnings.warn("Another unmatched warning.", category)


def test_fail_to_match_future_warning():
category = FutureWarning
match = "Warning"
unmatched = (
r"Did not see warning 'FutureWarning' matching 'Warning'. "
r"The emitted warning messages are "
r"\[FutureWarning\('This is not a match.'\), "
r"FutureWarning\('Another unmatched warning.'\)\]"
)
with pytest.raises(AssertionError, match=unmatched):
with tm.assert_produces_warning(category, match=match):
warnings.warn("This is not a match.", category)
warnings.warn("Another unmatched warning.", category)


def test_fail_to_match_resource_warning():
category = ResourceWarning
match = r"\d+"
unmatched = (
r"Did not see warning 'ResourceWarning' matching '\\d\+'. "
r"The emitted warning messages are "
r"\[ResourceWarning\('This is not a match.'\), "
r"ResourceWarning\('Another unmatched warning.'\)\]"
)
with pytest.raises(AssertionError, match=unmatched):
with tm.assert_produces_warning(category, match=match):
warnings.warn(message, category)
warnings.warn("This is not a match.", category)
warnings.warn("Another unmatched warning.", category)


def test_fail_to_catch_actual_warning(pair_different_warnings):
Expand Down