@@ -94,19 +94,49 @@ def test_catch_warning_category_and_match(category, message, match):
94
94
warnings .warn (message , category )
95
95
96
96
97
- @pytest .mark .parametrize (
98
- "message, match" ,
99
- [
100
- ("Warning message" , "Not this message" ),
101
- ("Warning message" , "warning" ),
102
- ("Warning message" , r"\d+" ),
103
- ],
104
- )
105
- def test_fail_to_match (category , message , match ):
106
- msg = f"Did not see warning { repr (category .__name__ )} matching"
107
- with pytest .raises (AssertionError , match = msg ):
97
+ def test_fail_to_match_runtime_warning ():
98
+ category = RuntimeWarning
99
+ match = "Did not see this warning"
100
+ unmatched = (
101
+ r"Did not see warning 'RuntimeWarning' matching 'Did not see this warning'. "
102
+ r"The emitted warning messages are "
103
+ r"\[RuntimeWarning\('This is not a match.'\), "
104
+ r"RuntimeWarning\('Another unmatched warning.'\)\]"
105
+ )
106
+ with pytest .raises (AssertionError , match = unmatched ):
107
+ with tm .assert_produces_warning (category , match = match ):
108
+ warnings .warn ("This is not a match." , category )
109
+ warnings .warn ("Another unmatched warning." , category )
110
+
111
+
112
+ def test_fail_to_match_future_warning ():
113
+ category = FutureWarning
114
+ match = "Warning"
115
+ unmatched = (
116
+ r"Did not see warning 'FutureWarning' matching 'Warning'. "
117
+ r"The emitted warning messages are "
118
+ r"\[FutureWarning\('This is not a match.'\), "
119
+ r"FutureWarning\('Another unmatched warning.'\)\]"
120
+ )
121
+ with pytest .raises (AssertionError , match = unmatched ):
122
+ with tm .assert_produces_warning (category , match = match ):
123
+ warnings .warn ("This is not a match." , category )
124
+ warnings .warn ("Another unmatched warning." , category )
125
+
126
+
127
+ def test_fail_to_match_resource_warning ():
128
+ category = ResourceWarning
129
+ match = r"\d+"
130
+ unmatched = (
131
+ r"Did not see warning 'ResourceWarning' matching '\\d\+'. "
132
+ r"The emitted warning messages are "
133
+ r"\[ResourceWarning\('This is not a match.'\), "
134
+ r"ResourceWarning\('Another unmatched warning.'\)\]"
135
+ )
136
+ with pytest .raises (AssertionError , match = unmatched ):
108
137
with tm .assert_produces_warning (category , match = match ):
109
- warnings .warn (message , category )
138
+ warnings .warn ("This is not a match." , category )
139
+ warnings .warn ("Another unmatched warning." , category )
110
140
111
141
112
142
def test_fail_to_catch_actual_warning (pair_different_warnings ):
0 commit comments