]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Move test skipping on import failure from package to function.
authorSteve Baker <sbaker@redhat.com>
Sun, 23 Sep 2012 21:08:20 +0000 (09:08 +1200)
committerSteve Baker <sbaker@redhat.com>
Sun, 23 Sep 2012 22:40:23 +0000 (10:40 +1200)
This gives a better test output on what has been skipped and why.

The unittest2.TestCase class has been removed because it is unused and
unittest2 is not available to the gates.

Change-Id: I1b5ae0845b2ef8dadc5cff42770ef062ea4e862f

heat/tests/test_cfn.py
heat/tests/test_s3.py
heat/tests/utils.py

index 28b8842d71e41c2af2336161a3159cf89cb08179..af534d47ca140bdfe4c8851a49fd88ef0f70cb35 100644 (file)
@@ -27,15 +27,20 @@ from nose.plugins.attrib import attr
 from nose import with_setup
 import unittest
 import shutil
-from nose.exc import SkipTest
+
+from utils import skip_if
+
 try:
     from heat.cfntools.cfn_helper import *
 except:
-    raise SkipTest("unable to import cfn helper, skipping")
+    skip_test = True
+else:
+    skip_test = False
 
 
 @attr(tag=['unit', 'cfn_helper'])
 @attr(speed='fast')
+@skip_if(skip_test, 'unable to import cfn_helper')
 def test_boolean():
 
     assert(to_boolean('true'))
@@ -73,6 +78,7 @@ def tearDown_credential_file():
 @with_setup(setUp_credential_file, tearDown_credential_file)
 @attr(tag=['unit', 'cfn-hup'])
 @attr(speed='fast')
+@skip_if(skip_test, 'unable to import cfn_helper')
 def test_hup_conf1():
     good = """
 [main]
@@ -91,6 +97,7 @@ interval=3
 @with_setup(setUp_credential_file, tearDown_credential_file)
 @attr(tag=['unit', 'cfn-hup'])
 @attr(speed='fast')
+@skip_if(skip_test, 'unable to import cfn_helper')
 def test_hup_default():
     good = """
 [main]
@@ -107,6 +114,7 @@ credential-file=/tmp/incredible
 @with_setup(setUp_credential_file, tearDown_credential_file)
 @attr(tag=['unit', 'cfn-hup'])
 @attr(speed='fast')
+@skip_if(skip_test, 'unable to import cfn_helper')
 def test_hup_hook():
     good = """
 [main]
@@ -152,6 +160,7 @@ class MetadataTest(unittest.TestCase):
 
     @attr(tag=['unit', 'cfn-metadata'])
     @attr(speed='fast')
+    @skip_if(skip_test, 'unable to import cfn_helper')
     def test_metadata_files(self):
         j = ''' {
         "AWS::CloudFormation::Init" : {
@@ -238,6 +247,7 @@ class CommandRunnerTest(unittest.TestCase):
 
     @attr(tag=['unit', 'cfn-helper'])
     @attr(speed='fast')
+    @skip_if(skip_test, 'unable to import cfn_helper')
     def test_runas(self):
         import subprocess
         self.m.StubOutWithMock(subprocess, 'Popen')
@@ -251,6 +261,7 @@ class CommandRunnerTest(unittest.TestCase):
 
     @attr(tag=['unit', 'cfn-helper'])
     @attr(speed='fast')
+    @skip_if(skip_test, 'unable to import cfn_helper')
     def test_default_runas(self):
         import subprocess
         self.m.StubOutWithMock(subprocess, 'Popen')
index 1a7c5c4f44017205d440f2a62b03f5ebfbf966c3..86ab3e4e3954932a4ecde2b70b9e689537b3f3d9 100644 (file)
@@ -26,16 +26,20 @@ from nose.plugins.attrib import attr
 
 from heat.engine import s3
 from heat.engine import parser
-from nose.exc import SkipTest
+from utils import skip_if
+
 try:
-    from swiftclient import client as swiftclient
+    from swiftclients import client as swiftclient
 except:
-    raise SkipTest("unable to import swiftclient, skipping")
+    skip_test = True
+else:
+    skip_test = False
 
 
 @attr(tag=['unit', 'resource'])
 @attr(speed='fast')
 class s3Test(unittest.TestCase):
+    @skip_if(skip_test, 'unable to import swiftclient')
     def setUp(self):
         self.m = mox.Mox()
         self.m.CreateMock(swiftclient.Connection)
@@ -76,11 +80,13 @@ class s3Test(unittest.TestCase):
         self.assertEqual(s3.S3Bucket.CREATE_COMPLETE, resource.state)
         return resource
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_create_container_name(self):
         self.m.UnsetStubs()
         self.assertTrue(re.match(self.container_pattern,
              s3.S3Bucket._create_container_name('test_stack.test_resource')))
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_attributes(self):
         swiftclient.Connection.put_container(
             mox.Regex(self.container_pattern),
@@ -116,6 +122,7 @@ class s3Test(unittest.TestCase):
         resource.delete()
         self.m.VerifyAll()
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_public_read(self):
         swiftclient.Connection.put_container(
             mox.Regex(self.container_pattern),
@@ -133,6 +140,7 @@ class s3Test(unittest.TestCase):
         resource.delete()
         self.m.VerifyAll()
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_public_read_write(self):
         swiftclient.Connection.put_container(
             mox.Regex(self.container_pattern),
@@ -150,6 +158,7 @@ class s3Test(unittest.TestCase):
         resource.delete()
         self.m.VerifyAll()
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_authenticated_read(self):
         swiftclient.Connection.put_container(
             mox.Regex(self.container_pattern),
@@ -167,6 +176,7 @@ class s3Test(unittest.TestCase):
         resource.delete()
         self.m.VerifyAll()
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_website(self):
 
         swiftclient.Connection.put_container(
@@ -185,6 +195,7 @@ class s3Test(unittest.TestCase):
         resource.delete()
         self.m.VerifyAll()
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_delete_exception(self):
 
         swiftclient.Connection.put_container(
@@ -203,6 +214,7 @@ class s3Test(unittest.TestCase):
 
         self.m.VerifyAll()
 
+    @skip_if(skip_test, 'unable to import swiftclient')
     def test_delete_retain(self):
 
         # first run, with retain policy
index 745fbbe387b9930cc8dcca833c91f8dea3c0480c..055335857abb30426cc6a17fafb319e353af3692 100644 (file)
 #    under the License.
 
 
-import unittest2
+import nose.plugins.skip as skip
 
 
-class TestCase(unittest2.TestCase):
-    pass
+class skip_test(object):
+    """Decorator that skips a test."""
+    def __init__(self, msg):
+        self.message = msg
+
+    def __call__(self, func):
+        def _skipper(*args, **kw):
+            """Wrapped skipper function."""
+            raise skip.SkipTest(self.message)
+        _skipper.__name__ = func.__name__
+        _skipper.__doc__ = func.__doc__
+        return _skipper
+
+
+class skip_if(object):
+    """Decorator that skips a test if condition is true."""
+    def __init__(self, condition, msg):
+        self.condition = condition
+        self.message = msg
+
+    def __call__(self, func):
+        def _skipper(*args, **kw):
+            """Wrapped skipper function."""
+            if self.condition:
+                raise skip.SkipTest(self.message)
+            func(*args, **kw)
+        _skipper.__name__ = func.__name__
+        _skipper.__doc__ = func.__doc__
+        return _skipper
+
+
+class skip_unless(object):
+    """Decorator that skips a test if condition is not true."""
+    def __init__(self, condition, msg):
+        self.condition = condition
+        self.message = msg
+
+    def __call__(self, func):
+        def _skipper(*args, **kw):
+            """Wrapped skipper function."""
+            if not self.condition:
+                raise skip.SkipTest(self.message)
+            func(*args, **kw)
+        _skipper.__name__ = func.__name__
+        _skipper.__doc__ = func.__doc__
+        return _skipper