blob: 0738201111d9506352d7bd0c893f8d562a983ab0 [file] [log] [blame]
Jay Pipes32621f92012-01-05 20:41:40 -05001# 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 Daguef237ccb2013-01-04 15:19:14 -050018"""Common utilities used in testing."""
Jay Pipes32621f92012-01-05 20:41:40 -050019
ivan-zhu1feeb382013-01-24 10:14:39 +080020from testtools import TestCase
Jay Pipes32621f92012-01-05 20:41:40 -050021
22
23class 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 Kranzd3bb4052012-01-19 10:10:05 -050031 def _skipper(*args, **kw):
Jay Pipes32621f92012-01-05 20:41:40 -050032 """Wrapped skipper function."""
33 testobj = args[0]
34 if not getattr(testobj, self.attr, False):
ivan-zhu1feeb382013-01-24 10:14:39 +080035 raise TestCase.skipException(self.message)
Jay Pipes32621f92012-01-05 20:41:40 -050036 func(*args, **kw)
37 _skipper.__name__ = func.__name__
38 _skipper.__doc__ = func.__doc__
39 return _skipper