Skip to content

Commit 3d0e362

Browse files
author
Ben Cipollini
committed
Add deprecation tests
1 parent 69f797b commit 3d0e362

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

nibabel/tests/test_checkwarns.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22
"""
33
from __future__ import division, print_function, absolute_import
44

5-
from ..checkwarns import ErrorWarnings, IgnoreWarnings
6-
75
from nose.tools import assert_true, assert_equal, assert_raises
86
from ..testing import (error_warnings, suppress_warnings,
97
clear_and_catch_warnings)
8+
9+
10+
def test_ignore_and_error_warnings():
11+
with clear_and_catch_warnings() as w:
12+
from ..checkwarns import ErrorWarnings, IgnoreWarnings
13+
assert_equal(len(w), 1)
14+
assert_equal(w[0].category, FutureWarning)
15+
16+
with clear_and_catch_warnings() as w:
17+
with IgnoreWarnings():
18+
pass
19+
assert_equal(len(w), 1)
20+
assert_equal(w[0].category, FutureWarning)
21+
22+
with clear_and_catch_warnings() as w:
23+
with ErrorWarnings():
24+
pass
25+
assert_equal(len(w), 1)
26+
assert_equal(w[0].category, FutureWarning)

nibabel/tests/test_testing.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,45 @@ def test_assert_allclose_safely():
6060
assert_allclose_safely([], [])
6161

6262

63+
def test_clear_and_catch_warnings():
64+
# Initial state of module, no warnings
65+
my_mod = _get_fresh_mod()
66+
assert_equal(getattr(my_mod, '__warningregistry__', {}), {})
67+
with clear_and_catch_warnings(modules=[my_mod]):
68+
warnings.simplefilter('ignore')
69+
warnings.warn('Some warning')
70+
assert_equal(my_mod.__warningregistry__, {})
71+
# Without specified modules, don't clear warnings during context
72+
with clear_and_catch_warnings():
73+
warnings.simplefilter('ignore')
74+
warnings.warn('Some warning')
75+
assert_warn_len_equal(my_mod, 1)
76+
# Confirm that specifying module keeps old warning, does not add new
77+
with clear_and_catch_warnings(modules=[my_mod]):
78+
warnings.simplefilter('ignore')
79+
warnings.warn('Another warning')
80+
assert_warn_len_equal(my_mod, 1)
81+
# Another warning, no module spec does add to warnings dict, except on
82+
# Python 3.4 (see comments in `assert_warn_len_equal`)
83+
with clear_and_catch_warnings():
84+
warnings.simplefilter('ignore')
85+
warnings.warn('Another warning')
86+
assert_warn_len_equal(my_mod, 2)
87+
88+
89+
class my_cacw(clear_and_catch_warnings):
90+
class_modules = (sys.modules[__name__],)
91+
92+
93+
def test_clear_and_catch_warnings_inherit():
94+
# Test can subclass and add default modules
95+
my_mod = _get_fresh_mod()
96+
with my_cacw():
97+
warnings.simplefilter('ignore')
98+
warnings.warn('Some warning')
99+
assert_equal(my_mod.__warningregistry__, {})
100+
101+
63102
def test_warn_error():
64103
# Check warning error context manager
65104
n_warns = len(filters)

0 commit comments

Comments
 (0)