tests: support standard unittest.SkipTest exceptions
authorMike Frysinger <vapier@gentoo.org>
Thu, 17 Oct 2013 05:00:56 +0000 (01:00 -0400)
committerMike Frysinger <vapier@gentoo.org>
Thu, 17 Oct 2013 05:00:56 +0000 (01:00 -0400)
pym/portage/tests/__init__.py

index 890851b9dacef069b1e2a54c93ff27f83d88a0a6..6c5c4dfd5203481141945ce6c416305b97d162d6 100644 (file)
@@ -13,6 +13,14 @@ try:
 except ImportError:
        from unittest import _TextTestResult
 
+try:
+       # They added the skip framework to python-2.7.
+       # Drop this once we drop python-2.6 support.
+       unittest_skip_shims = False
+       import unittest.SkipTest as SkipTest # new in python-2.7
+except ImportError:
+       unittest_skip_shims = True
+
 import portage
 from portage import os
 from portage import _encodings
@@ -188,10 +196,14 @@ class TestCase(unittest.TestCase):
                        except:
                                result.addError(self, sys.exc_info())
                                return
+
                        ok = False
                        try:
                                testMethod()
                                ok = True
+                       except SkipTest as e:
+                               result.addPortageSkip(self, "%s: SKIP: %s" %
+                                       (testMethod, str(e)))
                        except self.failureException:
                                if self.portage_skip is not None:
                                        if self.portage_skip is True:
@@ -207,6 +219,7 @@ class TestCase(unittest.TestCase):
                                raise
                        except:
                                result.addError(self, sys.exc_info())
+
                        try:
                                self.tearDown()
                        except SystemExit:
@@ -216,7 +229,8 @@ class TestCase(unittest.TestCase):
                        except:
                                result.addError(self, sys.exc_info())
                                ok = False
-                       if ok: result.addSuccess(self)
+                       if ok:
+                               result.addSuccess(self)
                finally:
                        result.stopTest(self)
 
@@ -258,6 +272,16 @@ class TestCase(unittest.TestCase):
                if os.path.exists(path):
                        raise self.failureException('path exists when it should not: %s' % path)
 
+if unittest_skip_shims:
+       # Shim code for <python-2.7.
+       class SkipTest(Exception):
+               """unittest.SkipTest shim for <python-2.7"""
+
+       def skipTest(self, reason):
+               raise SkipTest(reason)
+
+       setattr(TestCase, 'skipTest', skipTest)
+
 class TextTestRunner(unittest.TextTestRunner):
        """
        We subclass unittest.TextTestRunner to output SKIP for tests that fail but are skippable