@@ -60,6 +60,45 @@ def test_assert_allclose_safely():
60
60
assert_allclose_safely ([], [])
61
61
62
62
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
+
63
102
def test_warn_error ():
64
103
# Check warning error context manager
65
104
n_warns = len (filters )
0 commit comments