def get_internal_test_data(project, full_test_case_name, repr_strings=False): """Get test data defined inside the test itself.""" # check if test has data variable defined data_list = [] tc_name, parents = utils.separate_file_from_parents(full_test_case_name) path = os.path.join(session.testdir, 'projects', project, 'tests', os.sep.join(parents), '{}.py'.format(tc_name)) test_module, _ = utils.import_module(path) if hasattr(test_module, 'data'): msg_not_list_not_dict = ('Warning: infile test data must be a dictionary ' 'or a list of dictionaries\nCurrent value is:\n{}\n') data_variable = getattr(test_module, 'data') if type(data_variable) == dict: data_list.append(data_variable) elif type(data_variable) == list: if all(isinstance(item, dict) for item in data_variable): data_list = data_variable else: print(msg_not_list_not_dict.format(data_variable)) else: print(msg_not_list_not_dict.format(data_variable)) if repr_strings: # replace string values for their repr values data_list_clean = [] for data_dict in data_list: d = {} for k, v in data_dict.items(): if type(v) == str: d[k] = repr(v) else: d[k] = v data_list_clean.append(d) data_list = data_list_clean return data_list
def test_case_view(project, test_name): test = Test(project, test_name) if not test.exists: abort(404, 'The test {} does not exist'.format(test_name)) tc_name, parents = utils.separate_file_from_parents(test_name) _, error = utils.import_module(test.path) if error: url = url_for('webapp.test_case_code_view', project=project, test_name=test_name) content = ('<h4>There are errors in the test</h4>' '<p>There are errors and the test cannot be displayed, ' 'open the test code editor to solve them.</p>' '<a class="btn btn-default" href="{}">Open Test Code</a>'. format(url)) return render_template('common_element_error.html', project=project, item_name=test_name, content=content) else: test_data = test_data_module.get_test_data(project, test_name, repr_strings=True) return render_template('test_builder/test_case.html', project=project, test_components=test.components, test_case_name=tc_name, full_test_case_name=test_name, test_data=test_data)
def test_case_view(project, test_case_name): # check if the file is locked # is_locked_by = lock.is_file_locked(root_path, project, test_case_name) # print(is_locked_by, g.user.username) # if is_locked_by and is_locked_by != g.user.username: # abort(404, 'This file is locked by someone else.') # else: test_exists = test_case.test_case_exists(test_execution.root_path, project, test_case_name) if not test_exists: abort(404, 'The test {} does not exist'.format(test_case_name)) tc_name, parents = utils.separate_file_from_parents(test_case_name) path = test_case.generate_test_case_path(root_path, project, test_case_name) _, error = utils.import_module(path) if error: url = url_for('test_case_code_view', project=project, test_case_name=test_case_name) content = ('<h4>There are errors in the test</h4>' '<p>There are errors and the test cannot be displayed, ' 'open the test code editor to solve them.</p>' '<a class="btn btn-default" href="{}">Open Test Code</a>' .format(url)) return render_template('common_element_error.html', project=project, item_name=test_case_name, content=content) else: test_case_contents = test_case.get_test_case_content(root_path, project, test_case_name) test_data = test_data_module.get_test_data(root_path, project, test_case_name, repr_strings=True) return render_template('test_builder/test_case.html', project=project, test_case_contents=test_case_contents, test_case_name=tc_name, full_test_case_name=test_case_name, test_data=test_data)
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 suite_code_view(project, suite): suite_obj = suite_module.Suite(project, suite) if not suite_obj.exists: abort(404, f'The suite {suite} does not exist') _, error = utils.import_module(suite_obj.path) return render_template('suite_code.html', project=project, suite_name=suite, code=suite_obj.code, error=error)
def page_view(project, full_page_name, no_sidebar=False): path = page_object.generate_page_path(root_path, project, full_page_name) page_exists_ = page_object.page_exists(test_execution.root_path, project, full_page_name) if not page_exists_: abort(404, 'The page {} does not exist'.format(full_page_name)) _, error = utils.import_module(path) if error: if no_sidebar: url = url_for('page_code_view_no_sidebar', project=project, full_page_name=full_page_name) else: url = url_for('page_code_view', project=project, full_page_name=full_page_name) content = ('<h4>There are errors in the page</h4>' '<p>There are errors and the page cannot be displayed, ' 'open the page code editor to solve them.</p>' '<a class="btn btn-default" href="{}">Open Page Code</a>' .format(url)) return render_template('common_element_error.html', project=project, item_name=full_page_name, content=content, no_sidebar=no_sidebar) else: page_data = page_object.get_page_object_content(project, full_page_name) return render_template('page_builder/page_object.html', project=project, page_object_data=page_data, page_name=full_page_name, no_sidebar=no_sidebar)
def test_case_view(project, test_case_name): # check if user has permissions for this project if not user.has_permissions_to_project(g.user.id, project, root_path, 'gui'): return render_template('not_permission.html') # check if the file is locked # is_locked_by = lock.is_file_locked(root_path, project, test_case_name) # print(is_locked_by, g.user.username) # if is_locked_by and is_locked_by != g.user.username: # abort(404, 'This file is locked by someone else.') # else: tc_name, parents = utils.separate_file_from_parents(test_case_name) path = test_case.generate_test_case_path(root_path, project, test_case_name) _, error = utils.import_module(path) if error: return render_template('test_builder/test_case_syntax_error.html', project=project, full_test_case_name=test_case_name) else: test_case_contents = test_case.get_test_case_content( root_path, project, test_case_name) test_data = test_data_module.get_test_data(root_path, project, test_case_name, repr_strings=True) return render_template('test_builder/test_case.html', project=project, test_case_contents=test_case_contents, test_case_name=tc_name, full_test_case_name=test_case_name, test_data=test_data)
def test_incorrect_syntax(self, filecontent, expected, dir_function): filename = 'test_python_syntax.py' filepath = os.path.join(dir_function.path, filename) with open(filepath, 'w') as f: f.write(filecontent) _, error = utils.import_module(filepath) assert expected in error
def get_suite_module(workspace, project, suite_name): """Get the module of a suite""" suite_name, parents = utils.separate_file_from_parents(suite_name) path = os.path.join(workspace, 'projects', project, 'suites', os.sep.join(parents), suite_name + '.py') suite_module, _ = utils.import_module(path) return suite_module
def page_code_view(project, page_name, no_sidebar=False): page = Page(project, page_name) if not page.exists: abort(404, f'The page {page_name} does not exist') _, error = utils.import_module(page.path) return render_template('page_builder/page_code.html', project=project, page_object_code=page.code, page_name=page_name, error=error, no_sidebar=no_sidebar)
def save_page_object_code(): if request.method == 'POST': project = request.json['project'] page_object_name = request.json['pageObjectName'] content = request.json['content'] path = page_object.page_file_path(project, page_object_name) page_object.save_page_object_code(project, page_object_name, content) _, error = utils.import_module(path) return json.dumps({'error': error})
def page_code_save(): project = request.json['project'] page_name = request.json['pageName'] content = request.json['content'] _verify_permissions(Permissions.STANDARD, project) path = Page(project, page_name).path page_module.edit_page_code(project, page_name, content) _, error = utils.import_module(path) return jsonify({'error': error})
def suite_code_save(): project = request.json['project'] suite_name = request.json['suiteName'] content = request.json['content'] _verify_permissions(Permissions.STANDARD, project) suite_module.edit_suite_code(project, suite_name, content) path = suite_module.Suite(project, suite_name).path _, error = utils.import_module(path) return jsonify({'error': error})
def page_code_view(project, full_page_name, no_sidebar=False): page_exists_ = page_object.page_exists(project, full_page_name) if not page_exists_: abort(404, 'The page {} does not exist'.format(full_page_name)) path = page_object.page_file_path(project, full_page_name) _, error = utils.import_module(path) page_object_code = page_object.get_page_object_code(path) return render_template('page_builder/page_object_code.html', project=project, page_object_code=page_object_code, page_name=full_page_name, error=error, no_sidebar=no_sidebar)
def test_code_save(): project = request.json['project'] test_name = request.json['testName'] table_test_data = request.json['testData'] content = request.json['content'] _verify_permissions(Permissions.STANDARD, project) test_module.edit_test_code(project, test_name, content, table_test_data) path = test_module.Test(project, test_name).path _, error = utils.import_module(path) return jsonify({'error': error})
def get_test_tags(project, full_test_case_name): result = [] tc_name, parents = utils.separate_file_from_parents(full_test_case_name) path = os.path.join(session.testdir, 'projects', project, 'tests', os.sep.join(parents), '{}.py'.format(tc_name)) test_module, _ = utils.import_module(path) if hasattr(test_module, 'tags'): result = getattr(test_module, 'tags') return result
def save_test_case_code(): if request.method == 'POST': project = request.json['project'] test_case_name = request.json['testCaseName'] table_test_data = request.json['testData'] content = request.json['content'] test_case.save_test_case_code(root_path, project, test_case_name, content, table_test_data) path = test_case.generate_test_case_path(root_path, project, test_case_name) _, error = utils.import_module(path) return json.dumps({'error': error})
def test_case_code_view(project, test_name): test = Test(project, test_name) if not test.exists: abort(404, f'The test {test_name} does not exist') _, error = utils.import_module(test.path) csv_data = test_data_module.get_csv_test_data(project, test_name) json_data = test_data_module.get_json_test_data_as_string(project, test_name) return render_template('test_builder/test_code.html', project=project, test_case_contents=test.code, test_case_name=test.stem_name, full_test_case_name=test_name, csv_data=csv_data, json_data=json_data, error=error)
def page_code_view(project, full_page_name, no_sidebar=False): if not user.has_permissions_to_project(g.user.id, project, root_path, 'gui'): return render_template('not_permission.html') path = page_object.generate_page_path(root_path, project, full_page_name) _, error = utils.import_module(path) page_object_code = page_object.get_page_object_code(path) return render_template('page_builder/page_object_code.html', project=project, page_object_code=page_object_code, page_name=full_page_name, error=error, no_sidebar=no_sidebar)
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 test_case_code_view(project, test_case_name): test_exists = test_case.test_case_exists(project, test_case_name) if not test_exists: abort(404, 'The test {} does not exist'.format(test_case_name)) tc_name, parents = utils.separate_file_from_parents(test_case_name) path = os.path.join(session.testdir, 'projects', project, 'tests', os.sep.join(parents), tc_name + '.py') test_case_contents = test_case.get_test_case_code(path) _, error = utils.import_module(path) external_data = test_data_module.get_external_test_data(project, test_case_name) test_data_setting = session.settings['test_data'] return render_template('test_builder/test_case_code.html', project=project, test_case_contents=test_case_contents, test_case_name=tc_name, full_test_case_name=test_case_name, test_data=external_data, test_data_setting=test_data_setting, error=error)
def test_code_save(): project = request.json['project'] test_name = request.json['testName'] test_data = request.json['testData'] content = request.json['content'] _verify_permissions(Permissions.STANDARD, project) test_error = None data_errors = [] if test_data['json']: data_errors.extend(utils.json_parse_error(test_data['json'])) if not data_errors: test_module.edit_test_code(project, test_name, content, test_data) path = test_module.Test(project, test_name).path _, test_error = utils.import_module(path) return jsonify({'testError': test_error, 'dataErrors': data_errors})
def test_case_code_view(project, test_name): test = Test(project, test_name) if not test.exists: abort(404, 'The test {} does not exist'.format(test_name)) _, error = utils.import_module(test.path) external_data = test_data_module.get_external_test_data(project, test_name) test_data_setting = session.settings['test_data'] return render_template('test_builder/test_case_code.html', project=project, test_case_contents=test.code, test_case_name=test.stem_name, full_test_case_name=test_name, test_data=external_data, test_data_setting=test_data_setting, error=error)
def page_view(project, full_page_name, no_sidebar=False): if not user.has_permissions_to_project(g.user.id, project, root_path, 'gui'): return render_template('not_permission.html') path = page_object.generate_page_path(root_path, project, full_page_name) _, error = utils.import_module(path) if error: return render_template('page_builder/page_syntax_error.html', project=project, full_page_name=full_page_name) else: page_data = page_object.get_page_object_content(project, full_page_name) return render_template('page_builder/page_object.html', project=project, page_object_data=page_data, page_name=full_page_name, no_sidebar=no_sidebar)
def test_case_code_view(project, test_case_name): # check if user has permissions for this project if not user.has_permissions_to_project(g.user.id, project, root_path, 'gui'): return render_template('not_permission.html') tc_name, parents = utils.separate_file_from_parents(test_case_name) path = os.path.join(root_path, 'projects', project, 'tests', os.sep.join(parents), tc_name + '.py') test_case_contents = test_case.get_test_case_code(path) _, error = utils.import_module(path) external_data = test_data_module.get_external_test_data(root_path, project, test_case_name) test_data_setting = test_execution.settings['test_data'] return render_template('test_builder/test_case_code.html', project=project, test_case_contents=test_case_contents, test_case_name=tc_name, full_test_case_name=test_case_name, test_data=external_data, test_data_setting=test_data_setting, error=error)
def test_import_module_success(self, dir_function): filename = 'python_module.py' filepath = os.path.join(dir_function.path, filename) filecontent = ('foo = 2\n' 'def bar(a, b):\n' ' baz = ""\n') with open(filepath, 'w') as f: f.write(filecontent) module, error = utils.import_module(filepath) assert isinstance(module, types.ModuleType) assert error is None foo = getattr(module, 'foo') assert type(foo) == int bar = getattr(module, 'bar') assert isinstance(bar, types.FunctionType) args = list(inspect.signature(bar).parameters) code = inspect.getsource(bar) assert args == ['a', 'b'] assert code == 'def bar(a, b):\n baz = ""\n'
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 get_internal_test_data(workspace, project, full_test_case_name): """Get test data defined inside the test itself.""" # check if test has data variable defined data_list = [] tc_name, parents = utils.separate_file_from_parents(full_test_case_name) path = os.path.join(workspace, 'projects', project, 'tests', os.sep.join(parents), '{}.py'.format(tc_name)) test_module = utils.import_module(path) if hasattr(test_module, 'data'): data_variable = getattr(test_module, 'data') if type(data_variable) == dict: data_list.append(data_variable) elif type(data_variable) == list: if all(isinstance(item, dict) for item in data_variable): data_list = data_variable else: print(('Warning: infile test data must be a dictionary or ' 'a list of dictionaries\n' 'Current value is:\n' '{}\n' 'Test data for test {} will be ignored' .format(data_variable, full_test_case_name))) else: print(('Warning: infile test data must be a dictionary or ' 'a list of dictionaries\n' 'Current value is:\n' '{}\n' 'Test data for test {} will be ignored' .format(data_variable, full_test_case_name))) _ = [] for datax in data_list: d = {} for k,v in datax.items(): if type(v) == str and len(v): d[k] = "'{}'".format(v) else: d[k] = v _.append(d) data_list = _ return data_list
def import_page_into_test_module(base_path, parent_module, page_path_list): """Import a page module into a (test) module provided the relative dot path to the page. """ if len(page_path_list) > 1: new_node_name = page_path_list.pop(0) if not hasattr(parent_module, new_node_name): new_module = types.ModuleType(new_node_name) setattr(parent_module, new_node_name, new_module) else: new_module = getattr(parent_module, new_node_name) base_path = os.path.join(base_path, new_node_name) new_module = import_page_into_test_module(base_path, new_module, page_path_list) setattr(parent_module, new_node_name, new_module) else: path = os.path.join(base_path, page_path_list[0] + '.py') imported_module, error = utils.import_module(path) if error: raise ImportError(error) setattr(parent_module, page_path_list[0], imported_module) return parent_module
def test_module_local_functions_programatically(self, project_session, test_utils): """module imported from path""" _, project = project_session.activate() page_code = ('import sys\n' 'from os import walk\n' 'foo = 1\n' 'def bar():\n' ' pass\n' 'def _baz():\n' ' pass\n' 'class Traz:\n' ' pass') page_name = test_utils.create_random_page(project, page_code) module, _ = utils.import_module(Page(project, page_name).path) functions = utils.module_local_public_functions(module) assert len(functions) == 1 assert 'sys' not in functions assert 'walk' not in functions assert 'foo' not in functions assert '_baz' not in functions assert 'Traz' not in functions assert 'bar' in functions