From: Steve Baker Date: Sun, 23 Sep 2012 21:08:20 +0000 (+1200) Subject: Move test skipping on import failure from package to function. X-Git-Tag: 2014.1~1358 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=71324379f93c255f852d24e989b07aaf08358dc2;p=openstack-build%2Fheat-build.git Move test skipping on import failure from package to function. 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 --- diff --git a/heat/tests/test_cfn.py b/heat/tests/test_cfn.py index 28b8842d..af534d47 100644 --- a/heat/tests/test_cfn.py +++ b/heat/tests/test_cfn.py @@ -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') diff --git a/heat/tests/test_s3.py b/heat/tests/test_s3.py index 1a7c5c4f..86ab3e4e 100644 --- a/heat/tests/test_s3.py +++ b/heat/tests/test_s3.py @@ -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 diff --git a/heat/tests/utils.py b/heat/tests/utils.py index 745fbbe3..05533585 100644 --- a/heat/tests/utils.py +++ b/heat/tests/utils.py @@ -13,8 +13,52 @@ # 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