Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 18 | """Common utilities used in testing.""" |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 19 | |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 20 | from testtools import TestCase |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class skip_unless_attr(object): |
| 24 | """Decorator that skips a test if a specified attr exists and is True.""" |
| 25 | def __init__(self, attr, msg=None): |
| 26 | self.attr = attr |
| 27 | self.message = msg or ("Test case attribute %s not found " |
| 28 | "or False") % attr |
| 29 | |
| 30 | def __call__(self, func): |
David Kranz | d3bb405 | 2012-01-19 10:10:05 -0500 | [diff] [blame] | 31 | def _skipper(*args, **kw): |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 32 | """Wrapped skipper function.""" |
| 33 | testobj = args[0] |
| 34 | if not getattr(testobj, self.attr, False): |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 35 | raise TestCase.skipException(self.message) |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 36 | func(*args, **kw) |
| 37 | _skipper.__name__ = func.__name__ |
| 38 | _skipper.__doc__ = func.__doc__ |
| 39 | return _skipper |