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 4 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
17 changes: 17 additions & 0 deletions pandas/tests/util/test_assert_produces_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import warnings

import pytest

import pandas.util.testing as tm


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


@pytest.mark.filterwarnings('ignore:f1:FutureWarning')
@pytest.mark.filterwarnings('ignore:f2:RuntimeWarning')
def test_assert_produces_warning_honors_filter():
with tm.assert_produces_warning(RuntimeWarning):
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you be testing the new option here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes... I've messed something up, sorry.

f()
10 changes: 8 additions & 2 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2584,7 +2584,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 @@ -2597,6 +2597,7 @@ class for all warnings. To check that no warning is returned,
* "module" - print the warning the first time it is generated
from each module
* "once" - print the warning the first time it is generated
* None - do not apply a new filter

clear : str, default None
If not ``None`` then remove any previously raised warnings from
Expand Down Expand Up @@ -2646,7 +2647,12 @@ class for all warnings. To check that no warning is returned,
pass

saw_warning = False
warnings.simplefilter(filter_level)
if expected_warning and filter_level:
warnings.filterwarnings(filter_level, message='',
category=expected_warning)
elif filter_level:
# no expected warnings.
warnings.simplefilter(filter_level)
yield w
extra_warnings = []

Expand Down