Merge "Skip test cases by setUp method instead of setUpClass method."

This commit is contained in:
Zuul 2019-04-24 11:29:06 +00:00 committed by Gerrit Code Review
commit ae00fa2f6a
2 changed files with 41 additions and 10 deletions

View File

@ -43,7 +43,7 @@ def skip_until(reason, predicate, *args, **kwargs):
def skip_if_match(reason, match, predicate, *args, **kwargs):
def decorator(obj):
method, is_class_method = _get_decorated_method(obj)
method = _get_decorated_method(obj)
@functools.wraps(method)
def wrapped_method(*_args, **_kwargs):
@ -55,9 +55,6 @@ def skip_if_match(reason, match, predicate, *args, **kwargs):
if obj is method:
return wrapped_method
else:
if is_class_method:
wrapped_method = classmethod(wrapped_method)
setattr(obj, method.__name__, wrapped_method)
return obj
@ -67,13 +64,13 @@ def skip_if_match(reason, match, predicate, *args, **kwargs):
def _get_decorated_method(obj):
if inspect.isclass(obj):
if issubclass(obj, (unittest.TestCase, testtools.TestCase)):
return obj.setUpClass, True
return obj.setUp
elif _fixture.is_fixture(obj):
return obj.setUp, False
return obj.setUp
else:
raise TypeError("Cannot decorate class {!r}".format(obj))
else:
if callable(obj):
return obj, False
return obj
else:
raise TypeError("Cannot decorate object {!r}".format(obj))

View File

@ -45,17 +45,19 @@ class PositiveSkipMethodTest(unit.TobikoUnitTest):
self.fail('Not skipped')
class NegativeSkipMethodTest(unit.TobikoUnitTest):
class NegativeSkipBase(unit.TobikoUnitTest):
test_method_called = False
def setUp(self):
super(NegativeSkipMethodTest, self).setUp()
super(NegativeSkipBase, self).setUp()
self.addCleanup(self.assert_test_method_called)
def assert_test_method_called(self):
self.assertTrue(self.test_method_called)
class NegativeSkipMethodTest(NegativeSkipBase):
@tobiko.skip_if('condition value was false',
condition, False)
def test_skip_if_condition_called_with_args(self):
@ -209,3 +211,35 @@ class PositiveSkipUntilConditionCalledWithKwargsTest(unit.TobikoUnitTest):
def test_fail(self):
self.fail('Not skipped')
@tobiko.skip_if('condition value was true',
condition, False)
class NegativeSkipIfConditionCalledWithArgsTest(NegativeSkipBase):
def test_fail(self):
self.test_method_called = True
@tobiko.skip_if('condition value was true',
condition, value=False)
class NegativeSkipIfConditionCalledWithKwargsTest(NegativeSkipBase):
def test_fail(self):
self.test_method_called = True
@tobiko.skip_until('condition value was false',
condition, True)
class NegativeSkipUntilConditionCalledWithArgsTest(NegativeSkipBase):
def test_fail(self):
self.test_method_called = True
@tobiko.skip_until('condition value was false',
condition, value=True)
class NegativeSkipUntilConditionCalledWithKwargsTest(NegativeSkipBase):
def test_fail(self):
self.test_method_called = True