def load_testcase(raw_testcase): """ load testcase with api/testcase references. 用api/testcase引用加载testcase Args: raw_testcase (list): raw testcase content loaded from JSON/YAML file: [ # config part { "config": { "name": "XXXX", "base_url": "https://debugtalk.com" } }, # teststeps part { "test": {...} }, { "test": {...} } ] Returns: dict: loaded testcase content { "config": {}, "teststeps": [test11, test12] } """ JsonSchemaChecker.validate_testcase_v1_format(raw_testcase) config = {} tests = [] for item in raw_testcase: key, test_block = item.popitem() if key == "config": config.update(test_block) elif key == "test": tests.append(load_teststep(test_block)) else: logger.log_warning( "unexpected block key: {}. block key should only be 'config' or 'test'." .format(key)) return {"config": config, "teststeps": tests}
def load_testcase_v2(raw_testcase): """ load test_case in format version 2. Args: raw_testcase (dict): raw test_case content loaded from JSON/YAML file: { "config": { "name": "xxx", "variables": {} } "teststeps": [ { "name": "teststep 1", "request" {...} }, { "name": "teststep 2", "request" {...} }, ] } Returns: dict: loaded test_case content { "config": {}, "teststeps": [test11, test12] } """ JsonSchemaChecker.validate_testcase_v2_format(raw_testcase) raw_teststeps = raw_testcase.pop("teststeps") raw_testcase["teststeps"] = [ load_teststep(teststep) for teststep in raw_teststeps ] return raw_testcase
def load_test_file(path): """ load test file, file maybe test_case/testsuite/api Args: path (str): test file path Returns: dict: loaded test content # api { "path": path, "type": "api", "name": "", "request": {} } # test_case { "path": path, "type": "test_case", "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: test_case (format version 2) loaded_content = load_testcase_v2(raw_content) loaded_content["path"] = path loaded_content["type"] = "test_case" 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: test_case # make compatible with version < 2.2.0 loaded_content = load_testcase(raw_content) loaded_content["path"] = path loaded_content["type"] = "test_case" else: # invalid format raise exceptions.FileFormatError("Invalid test file format!") return loaded_content
def load_testsuite(raw_testsuite): """ load testsuite with test_case references. support two different formats. Args: raw_testsuite (dict): raw testsuite content loaded from JSON/YAML file: # version 1, compatible with version < 2.2.0 { "config": { "name": "xxx", "variables": {} } "testcases": { "testcase1": { "test_case": "/path/to/test_case", "variables": {...}, "parameters": {...} }, "testcase2": {} } } # version 2, implemented in 2.2.0 { "config": { "name": "xxx", "variables": {} } "testcases": [ { "name": "testcase1", "test_case": "/path/to/test_case", "variables": {...}, "parameters": {...} }, {} ] } Returns: dict: loaded testsuite content { "config": {}, "testcases": [testcase1, testcase2] } """ raw_testcases = raw_testsuite["testcases"] if isinstance(raw_testcases, dict): # format version 1, make compatible with version < 2.2.0 JsonSchemaChecker.validate_testsuite_v1_format(raw_testsuite) raw_testsuite["testcases"] = {} for name, raw_testcase in raw_testcases.items(): __extend_with_testcase_ref(raw_testcase) raw_testcase.setdefault("name", name) raw_testsuite["testcases"][name] = raw_testcase elif isinstance(raw_testcases, list): # format version 2, implemented in 2.2.0 JsonSchemaChecker.validate_testsuite_v2_format(raw_testsuite) raw_testsuite["testcases"] = {} for raw_testcase in raw_testcases: __extend_with_testcase_ref(raw_testcase) testcase_name = raw_testcase["name"] raw_testsuite["testcases"][testcase_name] = raw_testcase else: # invalid format raise exceptions.FileFormatError("Invalid testsuite format!") return raw_testsuite