def __extend_with_api_ref(raw_testinfo): """ extend with api reference Raises: exceptions.ApiNotFound: api not found """ api_name = raw_testinfo["api"] # api maybe defined in two types: # 1, individual file: each file is corresponding to one api definition # 2, api sets file: one file contains a list of api definitions if not os.path.isabs(api_name): # make compatible with Windows/Linux pwd = get_project_working_directory() api_path = os.path.join(pwd, *api_name.split("/")) if os.path.isfile(api_path): # type 1: api is defined in individual file api_name = api_path if api_name in tests_def_mapping["api"]: block = tests_def_mapping["api"][api_name] elif not os.path.isfile(api_name): raise exceptions.ApiNotFound("{} not found!".format(api_name)) else: block = load_file(api_name) # NOTICE: avoid project_mapping been changed during iteration. raw_testinfo["api_def"] = utils.deepcopy_dict(block) tests_def_mapping["api"][api_name] = block
def __extend_with_testcase_ref(raw_testinfo): """ extend with testcase reference """ testcase_path = raw_testinfo["testcase"] if testcase_path not in tests_def_mapping["testcases"]: # make compatible with Windows/Linux pwd = get_project_working_directory() testcase_path = os.path.join(pwd, *testcase_path.split("/")) loaded_testcase = load_file(testcase_path) if isinstance(loaded_testcase, list): # make compatible with version < 2.2.0 testcase_dict = load_testcase(loaded_testcase) elif isinstance(loaded_testcase, dict) and "teststeps" in loaded_testcase: # format version 2, implemented in 2.2.0 testcase_dict = load_testcase_v2(loaded_testcase) else: raise exceptions.FileFormatError( "Invalid format testcase: {}".format(testcase_path)) tests_def_mapping["testcases"][testcase_path] = testcase_dict else: testcase_dict = tests_def_mapping["testcases"][testcase_path] raw_testinfo["testcase_def"] = testcase_dict
def test_load_csv_file_one_parameter(self): csv_file_path = os.path.join(os.getcwd(), 'tests/data/user_agent.csv') csv_content = load.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_yaml_testcases(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testcase_hardcode.yml') testcases = load.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_multiple_parameters(self): csv_file_path = os.path.join(os.getcwd(), 'tests/data/account.csv') csv_content = load.load_file(csv_file_path) self.assertEqual(csv_content, [{ 'username': '******', 'password': '******' }, { 'username': '******', 'password': '******' }, { 'username': '******', 'password': '******' }])
def load_test_file(path): """ load test file, file maybe testcase/testsuite/api Args: path (str): test file path Returns: dict: loaded test content # api { "path": path, "type": "api", "name": "", "request": {} } # testcase { "path": path, "type": "testcase", "config": {}, "teststeps": [] } # testsuite { "path": path, "type": "testsuite", "config": {}, "testcases": {} } """ raw_content = load_file(path) if isinstance(raw_content, dict): if "testcases" in raw_content: # file_type: testsuite loaded_content = load_testsuite(raw_content) loaded_content["path"] = path loaded_content["type"] = "testsuite" elif "teststeps" in raw_content: # file_type: testcase (format version 2) loaded_content = load_testcase_v2(raw_content) loaded_content["path"] = path loaded_content["type"] = "testcase" elif "request" in raw_content: # file_type: api JsonSchemaChecker.validate_api_format(raw_content) loaded_content = raw_content loaded_content["path"] = path loaded_content["type"] = "api" else: # invalid format raise exceptions.FileFormatError("Invalid test file format!") elif isinstance(raw_content, list) and len(raw_content) > 0: # file_type: testcase # make compatible with version < 2.2.0 loaded_content = load_testcase(raw_content) loaded_content["path"] = path loaded_content["type"] = "testcase" else: # invalid format raise exceptions.FileFormatError("Invalid test file format!") return loaded_content
def test_load_testcases_bad_filepath(self): testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo') with self.assertRaises(exceptions.FileNotFound): load.load_file(testcase_file_path)