From 486deeabd695bfd06a2aaf3c047e7206cd39f61f Mon Sep 17 00:00:00 2001 From: Justin Shepherd Date: Thu, 4 Aug 2011 11:00:00 -0500 Subject: [PATCH] Implemented @utils.skip_test, @utils.skip_unless and @utils.skip_if functionality in glance/test/utils.py. Added glance/tests/unit/test_skip_examples.py which contains example skip case usages. Fixed issue where ./run_tests.sh would not execute pep8. Fixed couple of pep8 violations in test_skip_examples.py Change-Id: Id6eaa8768b663b4638fbca0e3bdf72b74969150a --- .gitignore | 4 ++ glance/store/registries.py | 2 +- glance/tests/unit/test_skip_examples.py | 48 ++++++++++++++++++++++++ glance/tests/utils.py | 50 +++++++++++++++++++++++++ run_tests.py | 2 +- run_tests.sh | 2 +- 6 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 glance/tests/unit/test_skip_examples.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..505ea84f20 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +*.log +.glance-venv +tests.sqlite diff --git a/glance/store/registries.py b/glance/store/registries.py index 0ba704adb1..f455bad4db 100644 --- a/glance/store/registries.py +++ b/glance/store/registries.py @@ -79,5 +79,5 @@ def lookup_by_registry(registry, image_id): adapter = REGISTRY_ADAPTERS[registry] except KeyError: raise UnknownImageRegistry("'%s' not found" % registry) - + return adapter.lookup(image_id) diff --git a/glance/tests/unit/test_skip_examples.py b/glance/tests/unit/test_skip_examples.py new file mode 100644 index 0000000000..21d55990ca --- /dev/null +++ b/glance/tests/unit/test_skip_examples.py @@ -0,0 +1,48 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010-2011 OpenStack, LLC +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest + +from glance.tests import utils + + +class TestSkipExamples(unittest.TestCase): + test_counter = 0 + + @utils.skip_test("Example usage of @utils.skip_test()") + def test_skip_test_example(self): + self.fail("skip_test failed to work properly.") + + @utils.skip_if(True, "Example usage of @utils.skip_if()") + def test_skip_if_example(self): + self.fail("skip_if failed to work properly.") + + @utils.skip_unless(False, "Example usage of @utils.skip_unless()") + def test_skip_unless_example(self): + self.fail("skip_unless failed to work properly.") + + @utils.skip_if(False, "This test case should never be skipped.") + def test_001_increase_test_counter(self): + TestSkipExamples.test_counter += 1 + + @utils.skip_unless(True, "This test case should never be skipped.") + def test_002_increase_test_counter(self): + TestSkipExamples.test_counter += 1 + + def test_003_verify_test_counter(self): + self.assertEquals(TestSkipExamples.test_counter, 2, + "Tests were not skipped appropriately") diff --git a/glance/tests/utils.py b/glance/tests/utils.py index b0da82ef08..897937cffb 100644 --- a/glance/tests/utils.py +++ b/glance/tests/utils.py @@ -21,6 +21,56 @@ import os import socket import subprocess +import nose.plugins.skip + + +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 nose.SkipTest(self.message) + _skipper.__name__ = func.__name__ + _skipper.__doc__ = func.__doc__ + return _skipper + + +class skip_if(object): + """Decorator that skips a test if contition 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 nose.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 nose.SkipTest(self.message) + func(*args, **kw) + _skipper.__name__ = func.__name__ + _skipper.__doc__ = func.__doc__ + return _skipper + def execute(cmd, raise_error=True): """ diff --git a/run_tests.py b/run_tests.py index 91aed18e80..77e4773c76 100644 --- a/run_tests.py +++ b/run_tests.py @@ -234,7 +234,7 @@ class GlanceTestResult(result.TextTestResult): if stream is not None: if self.showAll: message = [label] - detail = result._exception_details(err[1]) + detail = result._exception_detail(err[1]) if detail: message.append(detail) stream.writeln(": ".join(message)) diff --git a/run_tests.sh b/run_tests.sh index 99e1005d52..9764e42109 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -59,7 +59,7 @@ function run_pep8 { PEP8_EXCLUDE=vcsversion.py PEP8_OPTIONS="--exclude=$PEP8_EXCLUDE --repeat --show-pep8 --show-source" PEP8_INCLUDE="bin/* glance tools setup.py run_tests.py" - pep8 $PEP8_OPTIONS $PEP8_INCLUDE + ${wrapper} pep8 $PEP8_OPTIONS $PEP8_INCLUDE }