From 0decbecc357ab3b57d40c88bff21ee4f08c649de Mon Sep 17 00:00:00 2001 From: Calvin Storoschuk Date: Mon, 21 Jun 2021 13:24:10 -0400 Subject: [PATCH 1/5] TST: Add unmatched warning messages to assert_produces_warning() error --- pandas/_testing/_warnings.py | 11 ++++++++--- .../util/test_assert_produces_warning.py | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py index 5153118e9b142..5f01996d0390d 100644 --- a/pandas/_testing/_warnings.py +++ b/pandas/_testing/_warnings.py @@ -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): @@ -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( @@ -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}" ) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 45699fa1294d3..bbffcd60e0334 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -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}'\)" + 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): From 228c9cc331ecebe47297c34b67addbfdb00177a7 Mon Sep 17 00:00:00 2001 From: Calvin Storoschuk Date: Tue, 22 Jun 2021 09:09:51 -0400 Subject: [PATCH 2/5] remove logic from test and split into 3 --- .../util/test_assert_produces_warning.py | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index bbffcd60e0334..bd02a6d04e9f8 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -94,22 +94,40 @@ def test_catch_warning_category_and_match(category, message, match): warnings.warn(message, category) -@pytest.mark.parametrize( - "match", - [ - ("Not this message"), - ("Warning"), - (r"\d+"), - ], -) -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}'\)" +def test_fail_to_match_1(): + category = RuntimeWarning + match = "Did not see this warning" + unmatched = r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. "\ + r"The emitted warning messages are \[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_2(): + category = FutureWarning + match = "Warning" + unmatched = r"Did not see warning 'FutureWarning' matching 'Warning'. "\ + r"The emitted warning messages are \[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_3(): + category = ResourceWarning + match = r"\d+" + unmatched = r"Did not see warning 'ResourceWarning' matching '\\d\+'. "\ + r"The emitted warning messages are \[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(msg1, category) - warnings.warn(msg2, 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): From 1c2fe7315522aee4acc40033f9ba7586fda2ce3b Mon Sep 17 00:00:00 2001 From: Calvin Storoschuk Date: Tue, 22 Jun 2021 09:34:48 -0400 Subject: [PATCH 3/5] make test names more explicit and reformat --- .../util/test_assert_produces_warning.py | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index bd02a6d04e9f8..4ff7b09e707e4 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -94,36 +94,42 @@ def test_catch_warning_category_and_match(category, message, match): warnings.warn(message, category) -def test_fail_to_match_1(): +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'. "\ - r"The emitted warning messages are \[RuntimeWarning\('This is not a match.'\),"\ - r" RuntimeWarning\('Another unmatched warning.'\)\]" + unmatched = ( + r"Did not see warning 'RuntimeWarning' matching 'Did not see this " + r"warning'. The emitted warning messages are \[RuntimeWarning\('This is not a " + r"match.'\), 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_2(): +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 \[FutureWarning\('This is not a match.'\),"\ + unmatched = ( + r"Did not see warning 'FutureWarning' matching 'Warning'. " + r"The emitted warning messages are \[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_3(): +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 \[ResourceWarning\('This is not a match.'\),"\ - r" ResourceWarning\('Another unmatched warning.'\)\]" + unmatched = ( + r"Did not see warning 'ResourceWarning' matching '\\d\+'. " + r"The emitted warning messages are \[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("This is not a match.", category) From 5aebd52eb653cd866f741f897e915388122efc9a Mon Sep 17 00:00:00 2001 From: Calvin Storoschuk Date: Tue, 22 Jun 2021 10:09:32 -0400 Subject: [PATCH 4/5] reformat unmatched error message --- pandas/tests/util/test_assert_produces_warning.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index 4ff7b09e707e4..d059ea83bb4dc 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -98,8 +98,8 @@ 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 " - r"warning'. The emitted warning messages are \[RuntimeWarning\('This is not a " + r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. " + r"The emitted warning messages are \[RuntimeWarning\('This is not a " r"match.'\), RuntimeWarning\('Another unmatched warning.'\)\]" ) with pytest.raises(AssertionError, match=unmatched): @@ -113,8 +113,8 @@ def test_fail_to_match_future_warning(): match = "Warning" unmatched = ( r"Did not see warning 'FutureWarning' matching 'Warning'. " - r"The emitted warning messages are \[FutureWarning\('This is not a match.'\)," - r" FutureWarning\('Another unmatched warning.'\)\]" + r"The emitted warning messages are \[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): From 18c21944524216d0de34604e458b6cde755c5b8e Mon Sep 17 00:00:00 2001 From: Calvin Storoschuk Date: Thu, 24 Jun 2021 09:12:40 -0400 Subject: [PATCH 5/5] split unmatched warning into 4 lines in test --- pandas/tests/util/test_assert_produces_warning.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py index d059ea83bb4dc..e3eb083e1a383 100644 --- a/pandas/tests/util/test_assert_produces_warning.py +++ b/pandas/tests/util/test_assert_produces_warning.py @@ -99,8 +99,9 @@ def test_fail_to_match_runtime_warning(): match = "Did not see this warning" unmatched = ( r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. " - r"The emitted warning messages are \[RuntimeWarning\('This is not a " - r"match.'\), RuntimeWarning\('Another unmatched warning.'\)\]" + 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): @@ -113,7 +114,8 @@ def test_fail_to_match_future_warning(): match = "Warning" unmatched = ( r"Did not see warning 'FutureWarning' matching 'Warning'. " - r"The emitted warning messages are \[FutureWarning\('This is not a match.'\), " + r"The emitted warning messages are " + r"\[FutureWarning\('This is not a match.'\), " r"FutureWarning\('Another unmatched warning.'\)\]" ) with pytest.raises(AssertionError, match=unmatched): @@ -127,8 +129,9 @@ def test_fail_to_match_resource_warning(): match = r"\d+" unmatched = ( r"Did not see warning 'ResourceWarning' matching '\\d\+'. " - r"The emitted warning messages are \[ResourceWarning\('This is not a match.'\)" - r", ResourceWarning\('Another unmatched warning.'\)\]" + 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):