def _add_error(self, message, exception): """Add an error to the test from an exception. * Add a new step with `message`, don't log it * Add an error using: - message -> 'exception.__class__.__name__: exception' e.g.: 'AssertionError: expected title to be 'foo' - description -> traceback.format_exc() * Append the error to the last step * Log the error * Take a screenshot if screenshot_on_error == True and there is an open browser """ actions._add_step(message, log_step=False) error_message = f'{exception.__class__.__name__}: {exception}' trcbk = traceback.format_exc().rstrip() actions._add_error(message=error_message, description=trcbk) actions._append_error(message=error_message, description=trcbk) self._take_screeenshot_on_error()
def test(data): # append error when there are no steps with expected_exception(Exception, 'there is no last step to append error'): actions._append_error('error message', description='error description') # add a step and append an error to it actions._add_step('step message') actions._append_error('error message', description='error description') expected = {'message': 'error message', 'description': 'error description'} assert execution.steps[-1]['error'] == expected # append error when last step already contains an error with expected_exception(Exception, 'last step already contains an error'): actions._append_error('error message', description='error description')