def parameterize(self, csv_file_name, fetch_method="Sequential"): parameter_file_path = os.path.join(os.path.dirname(self.file_path), "{}".format(csv_file_name)) csv_content_list = FileUtils.load_file(parameter_file_path) if fetch_method.lower() == "random": random.shuffle(csv_content_list) return csv_content_list
def test_bugfix_type_match(self): testcase_file_path = os.path.join(os.getcwd(), 'tests/data/test_bugfix.yml') testcases = FileUtils.load_file(testcase_file_path) config_dict = {"path": testcase_file_path} self.test_runner.init_config(config_dict, "testset") test = testcases[2]["test"] self.test_runner.run_test(test)
def test_load_yaml_testcases(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testset_hardcode.yml') testcases = FileUtils.load_file(testcase_file_path) self.assertEqual(len(testcases), 3) test = testcases[0]["test"] self.assertIn('name', test) self.assertIn('request', test) self.assertIn('url', test['request']) self.assertIn('method', test['request'])
def test_load_csv_file_one_parameter(self): csv_file_path = os.path.join(os.getcwd(), 'tests/data/user_agent.csv') csv_content = FileUtils.load_file(csv_file_path) self.assertEqual(csv_content, [{ 'user_agent': 'iOS/10.1' }, { 'user_agent': 'iOS/10.2' }, { 'user_agent': 'iOS/10.3' }])
def test_load_csv_file_multiple_parameters(self): csv_file_path = os.path.join( os.getcwd(), 'tests/data/account.csv') csv_content = FileUtils.load_file(csv_file_path) self.assertEqual( csv_content, [ {'username': '******', 'password': '******'}, {'username': '******', 'password': '******'}, {'username': '******', 'password': '******'} ] )
def test_run_single_testcase(self): for testcase_file_path in self.testcase_file_path_list: testcases = FileUtils.load_file(testcase_file_path) config_dict = {"path": testcase_file_path} self.test_runner.init_config(config_dict, "testset") test = testcases[0]["test"] self.test_runner.run_test(test) test = testcases[1]["test"] self.test_runner.run_test(test) test = testcases[2]["test"] self.test_runner.run_test(test)
def load_api_file(file_path): """ load api definition from file and store in overall_def_dict["api"] api file should be in format below: [ { "api": { "def": "api_login", "request": {}, "validate": [] } }, { "api": { "def": "api_logout", "request": {}, "validate": [] } } ] """ api_items = FileUtils.load_file(file_path) if not isinstance(api_items, list): raise exception.FileFormatError( "API format error: {}".format(file_path)) for api_item in api_items: if not isinstance(api_item, dict) or len(api_item) != 1: raise exception.FileFormatError( "API format error: {}".format(file_path)) key, api_dict = api_item.popitem() if key != "api" or not isinstance(api_dict, dict) or "def" not in api_dict: raise exception.FileFormatError( "API format error: {}".format(file_path)) api_def = api_dict.pop("def") function_meta = parse_function(api_def) func_name = function_meta["func_name"] if func_name in TestcaseLoader.overall_def_dict["api"]: logger.log_warning( "API definition duplicated: {}".format(func_name)) api_dict["function_meta"] = function_meta TestcaseLoader.overall_def_dict["api"][func_name] = api_dict
def load_test_file(file_path): """ load testcase file or suite file @param file_path: absolute valid file path file_path should be in format below: [ { "config": { "name": "", "def": "suite_order()", "request": {} } }, { "test": { "name": "add product to cart", "api": "api_add_cart()", "validate": [] } }, { "test": { "name": "checkout cart", "request": {}, "validate": [] } } ] @return testset dict { "name": "desc1", "config": {}, "testcases": [testcase11, testcase12] } """ testset = { "name": "", "config": { "path": file_path }, "testcases": [] # TODO: rename to tests } for item in FileUtils.load_file(file_path): if not isinstance(item, dict) or len(item) != 1: raise exception.FileFormatError( "Testcase format error: {}".format(file_path)) key, test_block = item.popitem() if not isinstance(test_block, dict): raise exception.FileFormatError( "Testcase format error: {}".format(file_path)) if key == "config": testset["config"].update(test_block) testset["name"] = test_block.get("name", "") elif key == "test": if "api" in test_block: ref_call = test_block["api"] def_block = TestcaseLoader._get_block_by_name( ref_call, "api") TestcaseLoader._override_block(def_block, test_block) testset["testcases"].append(test_block) elif "suite" in test_block: ref_call = test_block["suite"] block = TestcaseLoader._get_block_by_name( ref_call, "suite") testset["testcases"].extend(block["testcases"]) else: testset["testcases"].append(test_block) else: logger.log_warning( "unexpected block key: {}. block key should only be 'config' or 'test'." .format(key)) return testset
def setUp(self): self.context = Context() testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml') self.testcases = FileUtils.load_file(testcase_file_path)
def setUp(self): self.context = Context() testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml') self.testcases = FileUtils.load_file(testcase_file_path)
def load_test_file(file_path): """ load testcase file or suite file @param file_path: absolute valid file path file_path should be in format below: [ { "config": { "name": "", "def": "suite_order()", "request": {} } }, { "test": { "name": "add product to cart", "api": "api_add_cart()", "validate": [] } }, { "test": { "name": "checkout cart", "request": {}, "validate": [] } } ] @return testset dict { "name": "desc1", "config": {}, "testcases": [testcase11, testcase12] } """ testset = { "name": "", "config": { "path": file_path }, "testcases": [] # TODO: rename to tests } for item in FileUtils.load_file(file_path): if not isinstance(item, dict) or len(item) != 1: raise exception.FileFormatError( "Testcase format error: {}".format(file_path)) key, test_block = item.popitem() if not isinstance(test_block, dict): raise exception.FileFormatError( "Testcase format error: {}".format(file_path)) if key == "config": testset["config"].update(test_block) testset["name"] = test_block.get("name", "") #add by zhengchun, can define runner if "runner" in test_block: # module_name, cls_name=item["config"]["runner"].split(".")[0:2] s = test_block["runner"] inx = s.rfind(".") if (inx <= 0): raise exception.ParamsError("runner format error[%s]" % (s)) module_name = s[0:inx] cls_name = s[inx + 1:] imported_module = utils.get_imported_module(module_name) ip_module_cls = getattr(imported_module, cls_name) testset["runner"] = ip_module_cls else: #defalut runner imported_module = utils.get_imported_module("runner") ip_module_cls = getattr(imported_module, "Runner") testset["runner"] = ip_module_cls elif key == "test": if "api" in test_block: ref_call = test_block["api"] def_block = TestcaseLoader._get_block_by_name( ref_call, "api") TestcaseLoader._override_block(def_block, test_block) testset["testcases"].append(test_block) elif "suite" in test_block: ref_call = test_block["suite"] block = TestcaseLoader._get_block_by_name( ref_call, "suite") testset["testcases"].extend(block["testcases"]) else: testset["testcases"].append(test_block) else: logger.log_warning( "unexpected block key: {}. block key should only be 'config' or 'test'." .format(key)) return testset
def test_load_testcases_bad_filepath(self): testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo') with self.assertRaises(exception.FileNotFoundError): FileUtils.load_file(testcase_file_path)