Skip to content

TST: assert_produces_warning works with filterwarnings #25721

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 5 commits into from
Mar 19, 2019
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
22 changes: 22 additions & 0 deletions pandas/tests/util/test_assert_produces_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import warnings

import pytest

import pandas.util.testing as tm


def f():
warnings.warn('f1', FutureWarning)
warnings.warn('f2', RuntimeWarning)


@pytest.mark.filterwarnings('ignore:f1:FutureWarning')
def test_assert_produces_warning_honors_filter():
# raise by default
with pytest.raises(AssertionError):
with tm.assert_produces_warning(RuntimeWarning):
f()

with tm.assert_produces_warning(RuntimeWarning,
raise_on_extra_warnings=False):
f()
14 changes: 10 additions & 4 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,8 @@ def exception_matches(self, exc_type, exc_value, trace_back):

@contextmanager
def assert_produces_warning(expected_warning=Warning, filter_level="always",
clear=None, check_stacklevel=True):
clear=None, check_stacklevel=True,
raise_on_extra_warnings=True):
"""
Context manager for running code expected to either raise a specific
warning, or not raise any warnings. Verifies that the code raises the
Expand All @@ -2584,7 +2585,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
The type of Exception raised. ``exception.Warning`` is the base
class for all warnings. To check that no warning is returned,
specify ``False`` or ``None``.
filter_level : str, default "always"
filter_level : str or None, default "always"
Specifies whether warnings are ignored, displayed, or turned
into errors.
Valid values are:
Expand All @@ -2608,6 +2609,9 @@ class for all warnings. To check that no warning is returned,
If True, displays the line that called the function containing
the warning to show were the function is called. Otherwise, the
line that implements the function is displayed.
raise_on_extra_warnings : bool, default True
Whether extra warnings not of the type `expected_warning` should
cause the test to fail.

Examples
--------
Expand Down Expand Up @@ -2676,8 +2680,10 @@ class for all warnings. To check that no warning is returned,
msg = "Did not see expected warning of class {name!r}.".format(
name=expected_warning.__name__)
assert saw_warning, msg
assert not extra_warnings, ("Caused unexpected warning(s): {extra!r}."
).format(extra=extra_warnings)
if raise_on_extra_warnings and extra_warnings:
raise AssertionError(
"Caused unexpected warning(s): {!r}.".format(extra_warnings)
)


class RNGContext(object):
Expand Down