def import_modules(self): if '/' in self.test_name: self.test_name = self.test_name.replace('/', '.') path = test_case.test_file_path(self.project, self.test_name, testdir=self.testdir) test_module, error = utils.import_module(path) if error: actions._add_error(message=error.splitlines()[-1], description=error) self.result['result'] = ResultsEnum.CODE_ERROR else: self.test_module = test_module # import logger into the test module setattr(self.test_module, 'logger', execution.logger) # import actions into the test module for action in dir(actions): setattr(self.test_module, action, getattr(actions, action)) # store test description if hasattr(self.test_module, 'description'): execution.description = self.test_module.description try: # import each page into the test_module if hasattr(self.test_module, 'pages'): base_path = os.path.join(self.testdir, 'projects', self.project, 'pages') for page in self.test_module.pages: self.test_module = import_page_into_test_module(base_path, self.test_module, page.split('.')) except Exception as e: message = '{}: {}'.format(e.__class__.__name__, e) trcbk = traceback.format_exc() actions._add_error(message=message, description=trcbk) self.result['result'] = ResultsEnum.CODE_ERROR if self.result['result'] == ResultsEnum.CODE_ERROR: self.finalize() else: self.run_setup()
def test(data): actions._add_error('error message', description='error description') expected = [{ 'message': 'error message', 'description': 'error description' }] assert execution.errors == expected actions._add_error('error message 2') expected = {'message': 'error message 2', 'description': ''} assert expected in execution.errors execution.errors = []
def import_modules(self): if '/' in self.test_name: self.test_name = self.test_name.replace('/', '.') path = Test(self.project, self.test_name).path test_module, error = utils.import_module(path) if error: actions._add_error(message=error.splitlines()[-1], description=error) self.result['result'] = ResultsEnum.CODE_ERROR else: self.test_module = test_module # import logger setattr(self.test_module, 'logger', execution.logger) # import actions module if self.settings['implicit_actions_import']: for action in dir(actions): setattr(self.test_module, action, getattr(actions, action)) # store test description if hasattr(self.test_module, 'description'): execution.description = self.test_module.description # import pages try: if hasattr(self.test_module, 'pages') and self.settings['implicit_page_import']: base_path = os.path.join(self.testdir, 'projects', self.project, 'pages') for page in self.test_module.pages: self.test_module = import_page_into_test(base_path, self.test_module, page.split('.')) except Exception as e: message = '{}: {}'.format(e.__class__.__name__, e) trcbk = traceback.format_exc() actions._add_error(message=message, description=trcbk) self.result['result'] = ResultsEnum.CODE_ERROR # check for skip flag # test is skipped only when run from a suite skip = getattr(self.test_module, 'skip', False) if skip and self.from_suite: self.result['result'] = ResultsEnum.SKIPPED msg = 'Skip: {}'.format(skip) if type(skip) is str else 'Skip' execution.logger.info(msg) if self.result['result'] in [ResultsEnum.CODE_ERROR, ResultsEnum.SKIPPED]: self.finalize() else: self.run_setup()
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 import_test(self): test_module, error = utils.import_module(self.test.path) if error: actions._add_error(message=error.splitlines()[-1], description=error) self.result = ResultsEnum.CODE_ERROR self.finalize(import_modules_failed=True) else: self.test_module = test_module # Gather test hooks defined in the test module # TODO setup is deprecated, if before_test is not present, and # setup is, use setup instead if hasattr(self.test_module, 'before_test'): self.test_hooks['before_test'].append(getattr(self.test_module, 'before_test')) elif hasattr(self.test_module, 'setup'): self.test_hooks['before_test'].append(getattr(self.test_module, 'setup')) if hasattr(self.test_module, 'before_each'): self.test_hooks['before_each'].append(getattr(self.test_module, 'before_each')) if hasattr(self.test_module, 'after_each'): self.test_hooks['after_each'].append(getattr(self.test_module, 'after_each')) # TODO teardown is deprecated, if after_test is not present, and # teardown is, use teardown instead if hasattr(self.test_module, 'after_test'): self.test_hooks['after_test'].append(getattr(self.test_module, 'after_test')) elif hasattr(self.test_module, 'teardown'): self.test_hooks['after_test'].append(getattr(self.test_module, 'teardown')) # If test_functions_to_run is empty every test function defined in the # file will be run if not self.test_functions_to_run: self.test_functions_to_run = self.test.test_function_list if not len(self.test_functions_to_run): msg = f'No tests were found for file: {self.test.name}' execution.logger.info(msg) self.finalize() return else: for test_function in self.test_functions_to_run: self.test_functions[test_function] = self._test_function_result_dict(test_function) self.import_modules()
def run_test(self): try: if hasattr(self.test_module, 'test'): self.test_module.test(execution.data) # take screenshot_on_end if self.settings['screenshot_on_end'] and execution.browser: actions.take_screenshot('Test end') else: error_msg = 'test {} does not have a test function'.format(self.test_name) actions._add_error(error_msg) self.result['result'] = ResultsEnum.CODE_ERROR except AssertionError as e: self._add_error(message='Failure', exception=e) self.result['result'] = ResultsEnum.FAILURE except Exception as e: if not self.result['result'] == ResultsEnum.FAILURE: self.result['result'] = ResultsEnum.CODE_ERROR self._add_error(message='Error', exception=e) self.run_teardown()
def import_modules(self): # import logger setattr(self.test_module, 'logger', execution.logger) # import actions module if self.settings['implicit_actions_import']: for action in utils.module_local_public_functions(actions): setattr(self.test_module, action, getattr(actions, action)) # store test description if hasattr(self.test_module, 'description'): execution.description = self.test_module.description # import pages try: if hasattr(self.test_module, 'pages') and self.settings['implicit_page_import']: base_path = self.project.page_directory_path for page in self.test_module.pages: self.test_module = import_page_into_test(base_path, self.test_module, page.split('.')) except Exception as e: message = f'{e.__class__.__name__}: {e}' trcbk = traceback.format_exc() actions._add_error(message=message, description=trcbk) self.result = ResultsEnum.CODE_ERROR # check for skip flag # test is skipped only when run from a suite skip = getattr(self.test_module, 'skip', False) if skip and self.from_suite: self.global_skip = skip if self.result == ResultsEnum.CODE_ERROR: self.finalize(import_modules_failed=True) else: self.run_setup()