class Test(unittest.TestCase):

    def test_resource_warning(self):
        s = socket.socket()
with warnings.catch_warnings(record=True) as w:
    warnings.resetwarnings()
warnings.simplefilter('ignore')
warnings.simplefilter('always', ResourceWarning)
del s
gc.collect()
self.assertFalse(w and str(w[-1]))
if __name__ == '__main__':
    unittest.main()
Преобразование Python 3 ResourceWarnings в исключение