def test_ensure_cleanup(self): test_list = [] with ensure_cleanup() as cleanup: if hasattr(list, 'clear'): cleanup.append(test_list.clear) else: def clear(): del test_list[:] cleanup.append(clear) test_list.append(1) self.assertIn(1, test_list) self.assertNotIn(1, test_list) self.assertEqual(len(test_list), 0)
def test_ensure_cleanup_failing_cleanup(self): def fail_cleanup(): raise Exception() with ensure_cleanup() as cleanup: cleanup.append(fail_cleanup)