Add replacement addCleanup.
authorJelmer Vernooij <jelmer@samba.org>
Tue, 27 Jan 2015 02:44:10 +0000 (03:44 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 6 Mar 2015 03:41:48 +0000 (04:41 +0100)
Change-Id: Ie85756effde344fc4cc9b693c4a28611ea091677
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/__init__.py

index 86ec3dff138654b9069fc0f816b34e4831aa823b..65a727aaf6c606e69ad9b475070b8efb08aeb009 100644 (file)
@@ -53,6 +53,7 @@ class TestCase(unittest.TestCase):
     def get_credentials(self):
         return cmdline_credentials
 
+    # These functions didn't exist before Python2.7:
     if not getattr(unittest.TestCase, "skipTest", None):
         def skipTest(self, reason):
             raise SkipTest(reason)
@@ -65,6 +66,16 @@ class TestCase(unittest.TestCase):
         def assertIsNot(self, a, b):
             self.assertTrue(a is not b)
 
+    if not getattr(unittest.TestCase, "addCleanup", None):
+        def addCleanup(self, fn, *args, **kwargs):
+            self._cleanups = getattr(self, "_cleanups", []) + [
+                (fn, args, kwargs)]
+
+        def tearDown(self):
+            super(TestCase, self).tearDown()
+            for (fn, args, kwargs) in reversed(getattr(self, "_cleanups", [])):
+                fn(*args, **kwargs)
+
 
 class LdbTestCase(unittest.TestCase):
     """Trivial test case for running tests against a LDB."""