Skip to content

Commit

Permalink
fix: reduce complexity in unittest tearDown method
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Mar 16, 2023
1 parent 33959c2 commit b210510
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions toolium/test/test_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ def test_tear_down_fail(logger):
# Check logging error messages
logger.info.assert_called_once_with('Running new test: %s', 'MockTestClass.mock_fail')
logger.error.assert_called_once_with("The test '%s' has failed: %s", 'MockTestClass.mock_fail', 'test error')
pass
25 changes: 14 additions & 11 deletions toolium/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,25 @@ def setUp(self):
self.logger.info("Running new test: %s", self.get_subclassmethod_name())

def tearDown(self):
# Get unit test exception
if hasattr(self._outcome, 'errors'):
exception_info = self._outcome.errors[-1][1] if len(self._outcome.errors) > 0 else None
else:
exception_info = self._outcome.result.failures[0] if len(self._outcome.result.failures) > 0 else None
exception = exception_info[1] if exception_info else None
# Get error message from unittest errors
error_message = ''
if hasattr(self._outcome, 'errors') and len(self._outcome.errors) > 0:
# Python <3.11
exception_info = self._outcome.errors[-1][1]
if exception_info:
exception = exception_info[1]
error_message = get_error_message_from_exception(exception)
elif not hasattr(self._outcome, 'errors') and len(self._outcome.result.failures) > 0:
# Python 3.11
traceback = self._outcome.result.failures[0][1]
error_message = get_error_message_from_traceback(traceback)

if not exception:
# Change test status
if not error_message:
self._test_passed = True
self.logger.info("The test '%s' has passed", self.get_subclassmethod_name())
else:
self._test_passed = False
if hasattr(self._outcome, 'errors'):
error_message = get_error_message_from_exception(exception)
else:
error_message = get_error_message_from_traceback(exception)
self.logger.error("The test '%s' has failed: %s", self.get_subclassmethod_name(), error_message)


Expand Down
5 changes: 1 addition & 4 deletions toolium/utils/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
"""

from os import makedirs
try:
from os import errno # Py <3.7
except ImportError:
import errno # Py 3.7+
import errno
import re

FILENAME_MAX_LENGTH = 100
Expand Down

0 comments on commit b210510

Please sign in to comment.