Context manager to check that no warnings are emitted. This context manager enables a given warning within its scope and checks that no warnings are emitted even with that warning enabled. If force_gc is True, a garbage collection is attempted before checking for warnings. This
(testcase, message='', category=Warning, force_gc=False)
| 119 | |
| 120 | @contextlib.contextmanager |
| 121 | def check_no_warnings(testcase, message='', category=Warning, force_gc=False): |
| 122 | """Context manager to check that no warnings are emitted. |
| 123 | |
| 124 | This context manager enables a given warning within its scope |
| 125 | and checks that no warnings are emitted even with that warning |
| 126 | enabled. |
| 127 | |
| 128 | If force_gc is True, a garbage collection is attempted before checking |
| 129 | for warnings. This may help to catch warnings emitted when objects |
| 130 | are deleted, such as ResourceWarning. |
| 131 | |
| 132 | Other keyword arguments are passed to warnings.filterwarnings(). |
| 133 | """ |
| 134 | from test.support import gc_collect |
| 135 | with warnings.catch_warnings(record=True) as warns: |
| 136 | warnings.filterwarnings('always', |
| 137 | message=message, |
| 138 | category=category) |
| 139 | yield |
| 140 | if force_gc: |
| 141 | gc_collect() |
| 142 | testcase.assertEqual(warns, []) |
| 143 | |
| 144 | |
| 145 | @contextlib.contextmanager |
no test coverage detected
searching dependent graphs…