Skip to content

Commit

Permalink
Skip decorator: apply skip dynamic conditions to setUp()
Browse files Browse the repository at this point in the history
There's currently a bug in cause by the fact that the decorator
function is always marked (__skip_method_decorator__) to be skipped,
even when the condition is supposed to be evaluated later, during
the execution of the decorator.

This has caused the setUp() method to not be executed in certain
cases, and the test method proceeds without finding its environment
set up (common situation when not running setUp(), one ends up with
missing attributes, in the class and cryptic errors during the
execution of the test).

This evaluates the same conditions used for skipping the test, but
also for skipping the setUp() method.  Unfortunately, we have to
distinguish between the condition and the decorator itself because
if we don't, we may end up running the decorated function (usually
the test) twice.

Signed-off-by: Cleber Rosa <crosa@redhat.com>
  • Loading branch information
clebergnu committed Apr 13, 2021
1 parent 452e0ed commit 3c3f30d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion avocado/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def wrapper(obj, *args, **kwargs): # pylint: disable=W0613
if condition:
raise core_exceptions.TestSkipError(message)
return function(obj, *args, **kwargs)
wrapper.__skip_test_decorator__ = True
wrapper.__skip_test_condition__ = condition
wrapper.__skip_test_condition_negate__ = negate
return wrapper


Expand Down
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
3 changes: 1 addition & 2 deletions selftests/functional/test_skiptests.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ class Skip(Base):
}

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


class NotSkip(Base):
Expand Down

0 comments on commit 3c3f30d

Please sign in to comment.