Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Expects no exception is raised in a context.
If the expectation is not met, the test is marked as fail after its
execution finishes.
A default message is added to the exception `details`.
Args:
message: string, custom message to add to exception's `details`.
extras: An optional field for extra information to be included in test
result.
"""
try:
yield
except Exception as e:
e_record = records.ExceptionRecord(e)
if extras:
e_record.extras = extras
msg = message or 'Got an unexpected exception'
details = '%s: %s' % (msg, e_record.details)
logging.exception(details)
e_record.details = details
recorder.add_error(e_record)
def _test_end(self, result, e):
"""Marks the end of the test logic.
Args:
result: One of the TEST_RESULT enums in TestResultEnums.
e: A test termination signal (usually an exception object). It can
be any exception instance or of any subclass of
mobly.signals.TestSignal.
"""
if self.begin_time is not None:
self.end_time = utils.get_current_epoch_time()
self.result = result
if e:
self.termination_signal = ExceptionRecord(e)
def __deepcopy__(self, memo):
"""Overrides deepcopy for the class.
If the exception object has a constructor that takes extra args, deep
copy won't work. So we need to have a custom logic for deepcopy.
"""
try:
exception = copy.deepcopy(self.exception)
except TypeError:
# If the exception object cannot be copied, use the original
# exception object.
exception = self.exception
result = ExceptionRecord(exception, self.position)
result.stacktrace = self.stacktrace
result.details = self.details
result.extras = copy.deepcopy(self.extras)
result.position = self.position
return result
would be the main error of the test record. Otherwise the newly added
error is added to the record's extra errors.
Args:
position: string, where this error occurred, e.g. 'teardown_test'.
e: An exception or a `signals.ExceptionRecord` object.
"""
if self.result != TestResultEnums.TEST_RESULT_FAIL:
self.result = TestResultEnums.TEST_RESULT_ERROR
if position in self.extra_errors:
raise Error('An exception is already recorded with position "%s",'
' cannot reuse.' % position)
if isinstance(e, ExceptionRecord):
self.extra_errors[position] = e
else:
self.extra_errors[position] = ExceptionRecord(e, position=position)
to the case where an uncaught exception happened.
If the test record has not recorded any error, the newly added error
would be the main error of the test record. Otherwise the newly added
error is added to the record's extra errors.
Args:
position: string, where this error occurred, e.g. 'teardown_test'.
e: An exception or a `signals.ExceptionRecord` object.
"""
if self.result != TestResultEnums.TEST_RESULT_FAIL:
self.result = TestResultEnums.TEST_RESULT_ERROR
if position in self.extra_errors:
raise Error('An exception is already recorded with position "%s",'
' cannot reuse.' % position)
if isinstance(e, ExceptionRecord):
self.extra_errors[position] = e
else:
self.extra_errors[position] = ExceptionRecord(e, position=position)