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

test status available for tearDown() #5224

Merged
merged 2 commits into from
Jan 12, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 62 additions & 67 deletions avocado/core/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def record_and_warn(*args, **kwargs):
self.__fail_reason = None
self.__fail_class = None
self.__traceback = None
self.__skip_test = False

# Are initialized lazily
self.__cache_dirs = None
Expand Down Expand Up @@ -703,40 +704,37 @@ def _check_reference(self, produced_file_path, reference_file_name,
return True
return False

def _run_avocado(self):
def _run_test(self):
"""
Auxiliary method to run_avocado.
Auxiliary method to run setup and test method.
"""
self._tag_start()
testMethod = getattr(self, self._testMethodName)
if self._config.get("run.test_runner") != 'nrunner':
self._start_logging()
if self.__sysinfo_enabled:
self.__sysinfo_logger.start()
test_exception = None
cleanup_exception = None
stdout_check_exception = None
stderr_check_exception = None
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))
self.__skip_test = not bool(skip_test_condition(self))
else:
skip_test = bool(skip_test_condition(self))
self.__skip_test = bool(skip_test_condition(self))
else:
if skip_test_condition_negate:
skip_test = not bool(skip_test_condition)
self.__skip_test = not bool(skip_test_condition)
else:
skip_test = bool(skip_test_condition)
self.__skip_test = bool(skip_test_condition)
else:
skip_test = bool(skip_test_condition)
self.__skip_test = bool(skip_test_condition)
try:
if skip_test is False:
if self.__skip_test is False:
self.__phase = 'SETUP'
self.setUp()
except exceptions.TestSkipError as details:
skip_test = True
self.__skip_test = True
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
raise exceptions.TestSkipError(details)
except exceptions.TestCancel as details:
Expand All @@ -762,51 +760,38 @@ def _run_avocado(self):
details = sys.exc_info()[1]
if not isinstance(details, Exception): # Avoid passing nasty exc
details = exceptions.TestError("%r: %s" % (details, details))
test_exception = details
self.log.debug("Local variables:")
local_vars = inspect.trace()[1][0].f_locals
for key, value in local_vars.items():
self.log.debug(' -> %s %s: %s', key, type(value), value)
finally:
try:
if skip_test is False:
self.__phase = 'TEARDOWN'
self.tearDown()
except exceptions.TestSkipError as details:
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
skip_illegal_msg = ('Using skip decorators in tearDown() '
'is not allowed in '
'avocado, you must fix your '
'test. Original skip exception: %s' %
details)
raise exceptions.TestError(skip_illegal_msg)
except exceptions.TestCancel as details:
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
raise
except: # avoid old-style exception failures pylint: disable=W0702
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
details = sys.exc_info()[1]
cleanup_exception = exceptions.TestSetupFail(details)

whiteboard_file = os.path.join(self.logdir, 'whiteboard')
genio.write_file(whiteboard_file, self.whiteboard)

# pylint: disable=E0702
if test_exception is not None:
raise test_exception
elif cleanup_exception is not None:
raise cleanup_exception
elif stdout_check_exception is not None:
raise stdout_check_exception
elif stderr_check_exception is not None:
raise stderr_check_exception
elif self.__log_warn_used:
raise exceptions.TestWarn("Test passed but there were warnings "
"during execution. Check the log for "
"details.")
raise details

self.__status = 'PASS'

def _tearDown(self):
"""
Auxiliary method to run tearDown.
"""
try:
if self.__skip_test is False:
self.__phase = 'TEARDOWN'
self.tearDown()
except exceptions.TestSkipError as details:
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
skip_illegal_msg = ('Using skip decorators in tearDown() '
'is not allowed in '
'avocado, you must fix your '
'test. Original skip exception: %s' %
details)
raise exceptions.TestError(skip_illegal_msg)
except exceptions.TestCancel:
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
raise
except: # avoid old-style exception failures pylint: disable=W0702
stacktrace.log_exc_info(sys.exc_info(), logger=LOG_JOB)
details = sys.exc_info()[1]
raise exceptions.TestSetupFail(details)

def _setup_environment_variables(self):
os.environ['AVOCADO_VERSION'] = VERSION
if self.basedir is not None:
Expand All @@ -819,16 +804,14 @@ def _setup_environment_variables(self):
if self.__sysinfo_enabled:
os.environ['AVOCADO_TEST_SYSINFODIR'] = self.__sysinfodir

def run_avocado(self):
"""
Wraps the run method, for execution inside the avocado runner.

:result: Unused param, compatibility with :class:`unittest.TestCase`.
"""
self._setup_environment_variables()
def _catch_test_status(self, method):
"""Wrapper around test methods for catching and logging failures."""
try:
self._tag_start()
self._run_avocado()
method()
if self.__log_warn_used:
raise exceptions.TestWarn("Test passed but there were warnings "
"during execution. Check the log for "
"details.")
except exceptions.TestBaseException as detail:
self.__status = detail.status
self.__fail_class = detail.__class__.__name__
Expand All @@ -852,13 +835,25 @@ def run_avocado(self):
"traceback for details.")
for e_line in tb_info:
self.log.error(e_line)
finally:
if self.__sysinfo_enabled:
self.__sysinfo_logger.end(self.__status)
self.__phase = 'FINISHED'
self._tag_end()
self._report()
self.log.info("")

def run_avocado(self):
"""
Wraps the run method, for execution inside the avocado runner.

:result: Unused param, compatibility with :class:`unittest.TestCase`.
"""
self._setup_environment_variables()
self._catch_test_status(self._run_test)
self._catch_test_status(self._tearDown)
whiteboard_file = os.path.join(self.logdir, 'whiteboard')
genio.write_file(whiteboard_file, self.whiteboard)
if self.__sysinfo_enabled:
self.__sysinfo_logger.end(self.__status)
self.__phase = 'FINISHED'
self._tag_end()
self._report()
self.log.info("")
if self._config.get("run.test_runner") != 'nrunner':
self._stop_logging()

def _report(self):
Expand Down
19 changes: 19 additions & 0 deletions selftests/.data/test_statuses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -33,6 +34,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -49,6 +51,7 @@ def test(self):
@skip("from tearDown()")
def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -65,6 +68,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -81,6 +85,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -96,6 +101,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.cancel()
self.log.info('teardown post')

Expand All @@ -113,6 +119,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -129,6 +136,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -144,6 +152,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.fail()
self.log.info('teardown post')

Expand All @@ -161,6 +170,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -177,6 +187,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -192,6 +203,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.warn('')
self.log.info('teardown post')

Expand All @@ -209,6 +221,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -225,6 +238,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -240,6 +254,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
sys.exit(-1)
self.log.info('teardown post')

Expand All @@ -258,6 +273,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -275,6 +291,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')


Expand All @@ -290,6 +307,7 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
raise ValueError
# pylint: disable=W0101
self.log.info('teardown post')
Expand All @@ -308,4 +326,5 @@ def test(self):

def tearDown(self):
self.log.info('teardown pre')
self.log.info('teardown status: %s', self.status)
self.log.info('teardown post')
Loading