def test_rename_directory(self, project_session, test_utils): testdir, project_name = project_session.activate() src = test_utils.random_string() test_utils.create_test(project_name, '{}.foo'.format(src)) dst = test_utils.random_string() project = Project(project_name) errors = project.rename_directory(src, dst, 'test') assert errors == [] srcpath = os.path.join(project.test_directory_path, src) dstpath = os.path.join(project.test_directory_path, dst) assert not os.path.isdir(srcpath) assert os.path.isdir(dstpath) assert not test.Test(project_name, '{}.foo'.format(src)).exists assert test.Test(project_name, '{}.foo'.format(dst)).exists
def create_test(project, name, content=None): if content is None: content = ('def test(data):\n' ' print("hello")\n') test.create_test(project, name) test.edit_test_code(project, name, content, table_test_data=[]) return test.Test(project, name).path
def get_internal_test_data_as_string(project, full_test_case_name): """Get test data defined inside the test itself.""" data_str = '' tm = test_module.Test(project, full_test_case_name).module if hasattr(tm, 'data'): data_variable = getattr(tm, 'data') data_str = format_internal_data_var(data_variable) return data_str
def test_get_internal_data_as_string_no_data_var(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.create_random_test(project) test_content = ("def test(data):\n" " pass\n") with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data_str = test_data.get_internal_test_data_as_string(project, test_name) assert data_str == ''
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 test_get_internal_data_not_dict_not_list_of_dicts(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.create_random_test(project) test_content = ("data = 'key1'\n" "def test(data):\n" " pass\n") with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data = test_data.get_internal_test_data(project, test_name) assert data == []
def test_delete_test_with_data(self, project_session, test_utils): """"test that when a test is deleted the data files are deleted as well """ _, project = project_session.activate() test_name = test_utils.create_random_test(project) data_path = os.path.splitext(test_module.Test(project, test_name).path)[0] + '.csv' open(data_path, 'x').close() errors = test_module.delete_test(project, test_name) assert errors == [] assert not os.path.isfile(data_path)
def run_command(project='', test_query='', browsers=None, processes=1, environments=None, interactive=False, timestamp=None, reports=None, report_folder=None, report_name=None, tags=None, cli_log_level=None): execution_runner = ExecutionRunner(browsers, processes, environments, interactive, timestamp, reports, report_folder, report_name, tags) if project: if test_directory.project_exists(project): execution_runner.project = project session.settings = settings_manager.get_project_settings(project) # add --interactive value to settings to make # it available from inside a test session.settings['interactive'] = interactive # override cli_log_level setting if provided by the CLI if cli_log_level: session.settings['cli_log_level'] = cli_log_level.upper() if test_query: norm_query = utils.normalize_query(test_query) if suite_module.Suite(project, norm_query).exists: execution_runner.run_suite(norm_query) elif test.Test(project, norm_query).exists: execution_runner.run_test(norm_query) else: if test_query == '.': test_query = '' path = os.path.join(session.testdir, 'projects', project, 'tests', test_query) if os.path.isdir(path): execution_runner.run_directory(test_query) else: msg = ('golem run: error: the value {} does not match ' 'an existing test, suite or directory'.format(test_query)) sys.exit(msg) else: print(messages.RUN_USAGE_MSG) tests = Project(project).test_tree print('Tests:') utils.display_tree_structure_command_line(tests['sub_elements']) suites = Project(project).suite_tree print('\nTest Suites:') # TODO print suites in structure for suite in suites['sub_elements']: print(' ' + suite['name']) else: msg = ('golem run: error: the project {} does not exist'.format(project)) sys.exit(msg) elif interactive: interactive_module.interactive(session.settings, browsers) else: print(messages.RUN_USAGE_MSG) print('Projects:') for project in test_directory.get_projects(): print(' {}'.format(project))
def test_duplicate_test_with_data_file(self, project_session, test_utils): """Assert when a test has a data file the data file is duplicated as well""" _, project = project_session.activate() test_name = test_utils.create_random_test(project) new_test_name = test_utils.random_string() data_path = os.path.splitext(Test(project, test_name).path)[0] + '.csv' with open(data_path, 'w+') as f: f.write('') new_data_path = os.path.splitext(test_module.Test(project, new_test_name).path)[0] + '.csv' test_module.duplicate_test(project, test_name, new_test_name) assert os.path.isfile(data_path) assert os.path.isfile(new_data_path)
def test_get_test_functions(self, project_class, test_utils): testdir, project = project_class.activate() test_name = 'test_file_one' content = ('def foo(data):\n' ' pass\n' 'def test_one(data):\n' ' pass\n' 'def test_two(data):\n' ' pass\n' 'test_three = 2\n') test_utils.create_test(project, test_name, content) test = test_module.Test(project, test_name) assert test.test_function_list == ['test_one', 'test_two']
def test_get_internal_data(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.create_random_test(project) test_content = ("data = [\n" " {'key1': 'a'},\n" " {'key1': 'b'},\n" "]\n" "def test(data):\n" " pass\n") with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data = test_data.get_internal_test_data(project, test_name) assert data == [{'key1': 'a'}, {'key1': 'b'}]
def get_internal_test_data(project, test_name): """Get test data defined inside the test itself. data var is ignored unless it's a dictionary or a list of dictionaries """ test = test_module.Test(project, test_name) if hasattr(test.module, 'data'): data_var = getattr(test.module, 'data') if type(data_var) is dict: return [data_var] if type(data_var) is list: if all(type(x) is dict for x in data_var): return data_var return []
def test_components(): project = request.args['project'] test_name = request.args['test'] _verify_permissions(Permissions.READ_ONLY, project) result = { 'error': '', 'components': [] } test = test_module.Test(project, test_name) if not test.exists: result['error'] = 'test does not exist' else: result['components'] = test.components return jsonify(result)
def test_edit_test_code_csv_data(self, project_session, test_utils): _, project = project_session.activate() test_data = [{'key': "'value'"}] settings_manager.save_project_settings(project, '{"test_data": "csv"}') test_name = test_utils.create_random_test(project) test_module.edit_test_code(project, test_name, SAMPLE_TEST_CONTENT, test_data) path = test_module.Test(project, test_name).path with open(path) as f: assert f.read() == SAMPLE_TEST_CONTENT path = os.path.join(Project(project).test_directory_path, test_name + '.csv') expected = ('key\n' '\'value\'\n') with open(path) as f: assert f.read() == expected
def test_get_test_data__json_and_internal(self, project_class, test_utils): """Only json is returned""" _, project = project_class.activate() test_name = test_utils.create_random_test(project) json_data = '[{"c": "d"}]' test_data.save_json_test_data(project, test_name, json_data) test_content = "data = {'e': 'f'}" with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data = test_data.get_parsed_test_data(project, test_name) assert data == json.loads(json_data) # remove json data, internal data is now returned test_data.remove_json_data_if_present(project, test_name) data = test_data.get_parsed_test_data(project, test_name) assert data == [{'e': 'f'}]
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_get_test_data(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.create_random_test(project) csv_data = [{'a': ' b'}] json_data = '[{"c": "d"}]' test_data.save_csv_test_data(project, test_name, csv_data) test_data.save_json_test_data(project, test_name, json_data) test_content = "data = {'e': 'f'}" with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data = test_data.get_test_data(project, test_name) expected = { 'csv': csv_data, 'json': json_data, 'internal': "data = {\n 'e': 'f',\n}" } assert data == expected
def test_set_execution_module_runner_values(self, project_class, test_utils): testdir, project = project_class.activate() test_name = test_utils.create_random_test(project) test = test_module.Test(project, test_name) report_directory = _mock_report_directory(testdir, project, test_name) settings = settings_manager.get_project_settings(project) browser = _define_browsers_mock(['chrome'])[0] test_data = {} secrets = {} env_name = 'foo' runner = test_runner.TestRunner(testdir, project, test_name, test_data, secrets, browser, env_name, settings, report_directory) runner._set_execution_module_values() from golem import execution attrs = [x for x in dir(execution) if not x.startswith('_')] assert len(attrs) == 20 assert execution.browser is None assert execution.browser_definition == browser assert execution.browsers == {} assert execution.steps == [] assert execution.data == {} assert execution.secrets == {} assert execution.description is None assert execution.errors == [] assert execution.settings == settings assert execution.test_name == test_name assert execution.test_dirname == test.dirname assert execution.test_path == test.path assert execution.project_name == project assert execution.project_path == test.project.path assert execution.testdir == testdir assert execution.report_directory == report_directory assert execution.logger is None assert execution.timers == {} assert execution.tags == [] assert execution.environment == env_name
def json_file_path(project, test_name): test = test_module.Test(project, test_name) return os.path.join(test.dirname, f'{test.stem_name}.json')
def test_set_execution_module_runner_values(self, project_module, test_utils): testdir, project = project_module.activate() test_file = test_utils.create_random_test(project) test = test_module.Test(project, test_file) timestamp = utils.get_timestamp() executiondir = _mock_report_directory(project, test_file, timestamp) testfile_reportdir = test_report.test_file_report_dir( test_file, execdir=executiondir) settings = settings_manager.get_project_settings(project) browser = _define_browsers_mock(['chrome'])[0] test_data = {} secrets = {} env_name = 'foo' runner = test_runner.TestRunner(testdir, project, test_file, test_data, secrets, browser, env_name, settings, executiondir, set_name='') runner._set_execution_module_values() from golem import execution attrs = [x for x in dir(execution) if not x.startswith('_')] assert len(attrs) == 23 assert execution.browser is None assert execution.browser_definition == browser assert execution.browsers == {} assert execution.data == {} assert execution.secrets == {} assert execution.description is None assert execution.settings == settings assert execution.test_file == test_file assert execution.test_dirname == test.dirname assert execution.test_path == test.path assert execution.project_name == project assert execution.project_path == test.project.path assert execution.testdir == testdir assert execution.execution_reportdir == executiondir assert execution.testfile_reportdir is None # Not populated at this point assert execution.logger is None assert execution.tags == [] assert execution.environment == env_name assert execution.test_name is None assert execution.steps == [] assert execution.errors == [] assert execution.test_reportdir is None assert execution.timers == {} # test _reset_execution_module_values_for_test_function execution.test_name = 'foo' execution.steps = ['foo'] execution.errors = ['foo'] execution.report_directory = 'foo' execution.timers = {'foo': 'bar'} runner._reset_execution_module_values_for_test_function( test_reportdir='x', test_name='test-name') assert execution.test_name == 'test-name' assert execution.steps == [] assert execution.errors == [] assert execution.test_reportdir == 'x' assert execution.timers == {}
def project_test_exists(): project = request.args['project'] test_name = request.args['test'] _verify_permissions(Permissions.READ_ONLY, project) return jsonify(test_module.Test(project, test_name).exists)