Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip Decorators: fix issues when setUp or the test function would/would not be run #4528

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 26 additions & 17 deletions avocado/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,47 @@ def wrapper(*args, **kwargs):
cancel_on = deco_factory("cancel", core_exceptions.TestCancel)


def _skip_method_decorator(function, message, condition):
def _skip_method_decorator(function, message, condition, negate):
"""Creates a skip decorator for a method."""
@wraps(function)
def wrapper(obj, *args, **kwargs): # pylint: disable=W0613
if callable(condition):
if condition(obj):
raise core_exceptions.TestSkipError(message)
elif condition:
raise core_exceptions.TestSkipError(message)
wrapper.__skip_test_decorator__ = True
if negate:
if callable(condition):
if not condition(obj):
raise core_exceptions.TestSkipError(message)
else:
if not condition:
raise core_exceptions.TestSkipError(message)
else:
if callable(condition):
if condition(obj):
raise core_exceptions.TestSkipError(message)
else:
if condition:
raise core_exceptions.TestSkipError(message)
return function(obj, *args, **kwargs)
wrapper.__skip_test_condition__ = condition
wrapper.__skip_test_condition_negate__ = negate
return wrapper


def _skip_class_decorator(cls, message, condition=None):
def _skip_class_decorator(cls, message, condition, negate):
"""Creates a skip decorator for a class."""
for key in cls.__dict__:
if key.startswith('test') and callable(getattr(cls, key)):
wrapped = _skip_method_decorator(getattr(cls, key),
message, condition)
message, condition, negate)
setattr(cls, key, wrapped)
return cls


def _get_skip_method_or_class_decorator(message, condition):
def _get_skip_method_or_class_decorator(message, condition, negate):
"""Returns a method or class decorator, according to decorated type."""
def decorator(obj):
if isinstance(obj, type):
return _skip_class_decorator(obj, message, condition)
return _skip_class_decorator(obj, message, condition, negate)
else:
return _skip_method_decorator(obj, message, condition)
return _skip_method_decorator(obj, message, condition, negate)
return decorator


Expand All @@ -110,7 +121,7 @@ def skip(message=None):
:type message: str

"""
return _get_skip_method_or_class_decorator(message, True)
return _get_skip_method_or_class_decorator(message, True, False)


def skipIf(condition, message=None):
Expand All @@ -124,7 +135,7 @@ class instance as a parameter
:param message: the message given when the test is skipped
:type message: str
"""
return _get_skip_method_or_class_decorator(message, condition)
return _get_skip_method_or_class_decorator(message, condition, False)


def skipUnless(condition, message=None):
Expand All @@ -138,6 +149,4 @@ class instance as a parameter
:param message: the message given when the test is skipped
:type message: str
"""
return _get_skip_method_or_class_decorator(
message,
lambda x: not (condition(x) if callable(condition) else condition))
return _get_skip_method_or_class_decorator(message, condition, True)
16 changes: 15 additions & 1 deletion avocado/core/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,21 @@ def _run_avocado(self):
output_check_exception = None
stdout_check_exception = None
stderr_check_exception = None
skip_test = getattr(testMethod, '__skip_test_decorator__', False)
skip_test_condition = getattr(testMethod, '__skip_test_condition__', False)
skip_test_condition_negate = getattr(testMethod, '__skip_test_condition_negate__', False)
if skip_test_condition:
if callable(skip_test_condition):
if skip_test_condition_negate:
skip_test = not bool(skip_test_condition(self))
else:
skip_test = bool(skip_test_condition(self))
else:
if skip_test_condition_negate:
skip_test = not bool(skip_test_condition)
else:
skip_test = bool(skip_test_condition)
else:
skip_test = bool(skip_test_condition)
try:
if skip_test is False:
self.__phase = 'SETUP'
Expand Down
14 changes: 9 additions & 5 deletions selftests/functional/test_skiptests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ def test5(self):
self.log.info('test executed')

@avocado.skipIf(lambda x: x.FALSE_CONDITION,
'skipIf with False condition, should not happen')
'skipIf with False condition should never happen')
def test6(self):
self.log.info('test executed')
# test "runs", because skipIf with False condition runs a test
self.cancel('ran, but was canceled')
self.log.info('test executed') # should never get here

@avocado.skipUnless(lambda x: x.TRUE_CONDITION,
'skipUnless with True condition, should not happen')
'skipUnless with True condition should never happen')
def test7(self):
self.log.info('test executed')
# test "runs", because skipUnless with True condition runs a test
self.cancel('ran, but was canceled')
self.log.info('test executed') # should never get here

def tearDown(self):
self.log.info('teardown executed')
Expand Down Expand Up @@ -309,7 +313,7 @@ class Skip(Base):
}

def test_skip_decorators(self):
self.check_skips_and_content(5)
self.check_status(skip=5, cancel=2)


class NotSkip(Base):
Expand Down