File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -1244,6 +1244,30 @@ def check_warnings(*filters, **kwargs):
1244
1244
return _filterwarnings (filters , quiet )
1245
1245
1246
1246
1247
+ @contextlib .contextmanager
1248
+ def check_no_warnings (testcase , message = '' , category = Warning , force_gc = False ):
1249
+ """Context manager to check that no warnings are emitted.
1250
+
1251
+ This context manager enables a given warning within its scope
1252
+ and checks that no warnings are emitted even with that warning
1253
+ enabled.
1254
+
1255
+ If force_gc is True, a garbage collection is attempted before checking
1256
+ for warnings. This may help to catch warnings emitted when objects
1257
+ are deleted, such as ResourceWarning.
1258
+
1259
+ Other keyword arguments are passed to warnings.filterwarnings().
1260
+ """
1261
+ with warnings .catch_warnings (record = True ) as warns :
1262
+ warnings .filterwarnings ('always' ,
1263
+ message = message ,
1264
+ category = category )
1265
+ yield
1266
+ if force_gc :
1267
+ gc_collect ()
1268
+ testcase .assertEqual (warns , [])
1269
+
1270
+
1247
1271
@contextlib .contextmanager
1248
1272
def check_no_resource_warning (testcase ):
1249
1273
"""Context manager to check that no ResourceWarning is emitted.
You can’t perform that action at this time.
0 commit comments