1+ """"
2+ Test module for testing ``pandas._testing.assert_produces_warning``.
3+ """
14import warnings
25
36import pytest
47
8+ from pandas .errors import DtypeWarning , PerformanceWarning
9+
510import pandas ._testing as tm
611
712
13+ @pytest .fixture (
14+ params = [
15+ RuntimeWarning ,
16+ ResourceWarning ,
17+ UserWarning ,
18+ FutureWarning ,
19+ DeprecationWarning ,
20+ PerformanceWarning ,
21+ DtypeWarning ,
22+ ],
23+ )
24+ def category (request ):
25+ """
26+ Return unique warning.
27+
28+ Useful for testing behavior of tm.assert_produces_warning with various categories.
29+ """
30+ return request .param
31+
32+
33+ @pytest .fixture (
34+ params = [
35+ (RuntimeWarning , UserWarning ),
36+ (UserWarning , FutureWarning ),
37+ (FutureWarning , RuntimeWarning ),
38+ (DeprecationWarning , PerformanceWarning ),
39+ (PerformanceWarning , FutureWarning ),
40+ (DtypeWarning , DeprecationWarning ),
41+ (ResourceWarning , DeprecationWarning ),
42+ (FutureWarning , DeprecationWarning ),
43+ ],
44+ ids = lambda x : type (x ).__name__ ,
45+ )
46+ def pair_different_warnings (request ):
47+ """
48+ Return pair or different warnings.
49+
50+ Useful for testing how several different warnings are handled
51+ in tm.assert_produces_warning.
52+ """
53+ return request .param
54+
55+
856def f ():
957 warnings .warn ("f1" , FutureWarning )
1058 warnings .warn ("f2" , RuntimeWarning )
@@ -20,3 +68,87 @@ def test_assert_produces_warning_honors_filter():
2068
2169 with tm .assert_produces_warning (RuntimeWarning , raise_on_extra_warnings = False ):
2270 f ()
71+
72+
73+ @pytest .mark .parametrize (
74+ "message, match" ,
75+ [
76+ ("" , None ),
77+ ("" , "" ),
78+ ("Warning message" , r".*" ),
79+ ("Warning message" , "War" ),
80+ ("Warning message" , r"[Ww]arning" ),
81+ ("Warning message" , "age" ),
82+ ("Warning message" , r"age$" ),
83+ ("Message 12-234 with numbers" , r"\d{2}-\d{3}" ),
84+ ("Message 12-234 with numbers" , r"^Mes.*\d{2}-\d{3}" ),
85+ ("Message 12-234 with numbers" , r"\d{2}-\d{3}\s\S+" ),
86+ ("Message, which we do not match" , None ),
87+ ],
88+ )
89+ def test_catch_warning_category_and_match (category , message , match ):
90+ with tm .assert_produces_warning (category , match = match ):
91+ warnings .warn (message , category )
92+
93+
94+ @pytest .mark .parametrize (
95+ "message, match" ,
96+ [
97+ ("Warning message" , "Not this message" ),
98+ ("Warning message" , "warning" ),
99+ ("Warning message" , r"\d+" ),
100+ ],
101+ )
102+ def test_fail_to_match (category , message , match ):
103+ msg = f"Did not see warning { repr (category .__name__ )} matching"
104+ with pytest .raises (AssertionError , match = msg ):
105+ with tm .assert_produces_warning (category , match = match ):
106+ warnings .warn (message , category )
107+
108+
109+ def test_fail_to_catch_actual_warning (pair_different_warnings ):
110+ expected_category , actual_category = pair_different_warnings
111+ match = "Did not see expected warning of class"
112+ with pytest .raises (AssertionError , match = match ):
113+ with tm .assert_produces_warning (expected_category ):
114+ warnings .warn ("warning message" , actual_category )
115+
116+
117+ def test_ignore_extra_warning (pair_different_warnings ):
118+ expected_category , extra_category = pair_different_warnings
119+ with tm .assert_produces_warning (expected_category , raise_on_extra_warnings = False ):
120+ warnings .warn ("Expected warning" , expected_category )
121+ warnings .warn ("Unexpected warning OK" , extra_category )
122+
123+
124+ def test_raise_on_extra_warning (pair_different_warnings ):
125+ expected_category , extra_category = pair_different_warnings
126+ match = r"Caused unexpected warning\(s\)"
127+ with pytest .raises (AssertionError , match = match ):
128+ with tm .assert_produces_warning (expected_category ):
129+ warnings .warn ("Expected warning" , expected_category )
130+ warnings .warn ("Unexpected warning NOT OK" , extra_category )
131+
132+
133+ def test_same_category_different_messages_first_match ():
134+ category = UserWarning
135+ with tm .assert_produces_warning (category , match = r"^Match this" ):
136+ warnings .warn ("Match this" , category )
137+ warnings .warn ("Do not match that" , category )
138+ warnings .warn ("Do not match that either" , category )
139+
140+
141+ def test_same_category_different_messages_last_match ():
142+ category = DeprecationWarning
143+ with tm .assert_produces_warning (category , match = r"^Match this" ):
144+ warnings .warn ("Do not match that" , category )
145+ warnings .warn ("Do not match that either" , category )
146+ warnings .warn ("Match this" , category )
147+
148+
149+ def test_right_category_wrong_match_raises (pair_different_warnings ):
150+ target_category , other_category = pair_different_warnings
151+ with pytest .raises (AssertionError , match = "Did not see warning.*matching" ):
152+ with tm .assert_produces_warning (target_category , match = r"^Match this" ):
153+ warnings .warn ("Do not match it" , target_category )
154+ warnings .warn ("Match this" , other_category )
0 commit comments