From 7d20f950689211b89d13b0512288725388bc1780 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 10 Dec 2018 15:57:06 +0800 Subject: [PATCH] refactor testcase layer mechanism: 1, autotest testsuite layer 2, performance test --- httprunner/api.py | 20 +- httprunner/loader.py | 280 +++++++++++++++++++--------- httprunner/parser.py | 286 +++++++++++++++++++++------- httprunner/runner.py | 4 +- httprunner/utils.py | 8 +- httprunner/validator.py | 8 +- tests/api/create_user.yml | 1 + tests/test_api.py | 121 ++++++------ tests/test_loader.py | 297 +++++++++++++++--------------- tests/test_parser.py | 50 ++--- tests/test_runner.py | 2 +- tests/test_validator.py | 2 +- tests/testsuites/create_users.yml | 23 +-- 13 files changed, 677 insertions(+), 425 deletions(-) diff --git a/httprunner/api.py b/httprunner/api.py index a9048750..136aaeb0 100644 --- a/httprunner/api.py +++ b/httprunner/api.py @@ -69,7 +69,7 @@ class HttpRunner(object): test_runner = runner.Runner(config, functions) TestSequense = type('TestSequense', (unittest.TestCase,), {}) - tests = testcase.get("tests", []) + tests = testcase.get("teststeps", []) for index, test_dict in enumerate(tests): for times_index in range(int(test_dict.get("times", 1))): # suppose one testcase should not have more than 9999 steps, @@ -80,7 +80,7 @@ class HttpRunner(object): loaded_testcase = self.test_loader.loadTestsFromTestCase(TestSequense) setattr(loaded_testcase, "config", config) - setattr(loaded_testcase, "tests", tests) + setattr(loaded_testcase, "teststeps", tests) setattr(loaded_testcase, "runner", test_runner) test_suite.addTest(loaded_testcase) @@ -145,14 +145,14 @@ class HttpRunner(object): """ # parse tests self.exception_stage = "parse tests" - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) if self.save_tests: - utils.dump_tests(tests_mapping, "parsed") + utils.dump_tests(parsed_tests_mapping, "parsed") # add tests to test suite self.exception_stage = "add tests to test suite" - test_suite = self._add_tests(tests_mapping) + test_suite = self._add_tests(parsed_tests_mapping) # run test suite self.exception_stage = "run test suite" @@ -236,16 +236,16 @@ def prepare_locust_tests(path): { "functions": {}, - "tests": [] + "teststeps": [] } """ tests_mapping = loader.load_tests(path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) - functions = tests_mapping.get("project_mapping", {}).get("functions", {}) - testcase = tests_mapping["testcases"][0] - items = testcase.get("tests", []) + functions = parsed_tests_mapping.get("project_mapping", {}).get("functions", {}) + testcase = parsed_tests_mapping["testcases"][0] + items = testcase.get("teststeps", []) tests = [] for item in items: diff --git a/httprunner/loader.py b/httprunner/loader.py index 4a2742ba..3bb2865d 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -286,75 +286,95 @@ tests_def_mapping = { "testcases": {} } -def load_test(raw_testinfo): - """ load test with api/testcase/proc references + +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): + api_path = os.path.join(tests_def_mapping["PWD"], api_name) + if os.path.isfile(api_path): + # type 1: api is defined in individual file + api_name = api_path + + try: + block = tests_def_mapping["api"][api_name] + # NOTICE: avoid project_mapping been changed during iteration. + raw_testinfo["api_def"] = utils.deepcopy_dict(block) + except KeyError: + raise exceptions.ApiNotFound("{} not found!".format(name)) + + +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"]: + testcase_path = os.path.join( + project_mapping["PWD"], + testcase_path + ) + testcase_dict = load_testcase(load_file(testcase_path)) + tests_def_mapping[testcase_path] = testcase_dict + else: + testcase_dict = tests_def_mapping[testcase_path] + + raw_testinfo["testcase_def"] = testcase_dict + + +def load_teststep(raw_testinfo): + """ load testcase step content. + teststep maybe defined directly, or reference api/testcase. Args: raw_testinfo (dict): test data, maybe in 3 formats. # api reference { "name": "add product to cart", - "api": "api_add_cart", - "variables": [], + "api": "/path/to/api", + "variables": {}, "validate": [], "extract": {} } # testcase reference { "name": "add product to cart", - "testcase": "create_and_check", - "variables": [] + "testcase": "/path/to/testcase", + "variables": {} } # define directly { "name": "checkout cart", "request": {}, - "variables": [], + "variables": {}, "validate": [], "extract": {} } Returns: - list: loaded tests list - - Args: - raw_testinfo (dict): test info + dict: loaded teststep content """ # reference api if "api" in raw_testinfo: - 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): - api_path = os.path.join(tests_def_mapping["PWD"], api_name) - if os.path.isfile(api_path): - # type 1: api is defined in individual file - api_name = api_path - - raw_testinfo["api_def"] = _get_api_definition(api_name) + __extend_with_api_ref(raw_testinfo) # TODO: reference proc functions - elif "func" in raw_testinfo: - pass + # elif "func" in raw_testinfo: + # pass # reference testcase elif "testcase" in raw_testinfo: - testcase_path = raw_testinfo["testcase"] - - if testcase_path not in tests_def_mapping["testcases"]: - testcase_path = os.path.join( - project_mapping["PWD"], - testcase_path - ) - testcase_dict = load_testcase(load_file(testcase_path)) - tests_def_mapping[testcase_path] = testcase_dict - else: - testcase_dict = tests_def_mapping[testcase_path] - - raw_testinfo["testcase_def"] = testcase_dict + __extend_with_testcase_ref(raw_testinfo) # define directly else: @@ -364,7 +384,7 @@ def load_test(raw_testinfo): def load_testcase(raw_testcase): - """ load testcase/testsuite with api/testcase references + """ load testcase with api/testcase references. Args: raw_testcase (list): raw testcase content loaded from JSON/YAML file: @@ -372,12 +392,11 @@ def load_testcase(raw_testcase): # config part { "config": { - "name": "", - "def": "suite_order()", - "request": {} + "name": "XXXX", + "base_url": "https://debugtalk.com" } }, - # tests part + # teststeps part { "test": {...} }, @@ -389,9 +408,8 @@ def load_testcase(raw_testcase): Returns: dict: loaded testcase content { - "name": "XYZ", "config": {}, - "tests": [test11, test12] + "teststeps": [test11, test12] } """ @@ -399,20 +417,11 @@ def load_testcase(raw_testcase): tests = [] for item in raw_testcase: - # TODO: add json schema validation - if not isinstance(item, dict) or len(item) != 1: - raise exceptions.FileFormatError("Testcase format error: {}".format(item)) - key, test_block = item.popitem() - if not isinstance(test_block, dict): - raise exceptions.FileFormatError("Testcase format error: {}".format(item)) - if key == "config": config.update(test_block) - elif key == "test": - tests.append(load_test(test_block)) - + tests.append(load_teststep(test_block)) else: logger.log_warning( "unexpected block key: {}. block key should only be 'config' or 'test'.".format(key) @@ -420,26 +429,113 @@ def load_testcase(raw_testcase): return { "config": config, - "tests": tests + "teststeps": tests } -def _get_api_definition(name): - """ get api definition by name. +def load_testsuite(raw_testsuite): + """ load testsuite with testcase references. + + Args: + raw_testsuite (dict): raw testsuite content loaded from JSON/YAML file: + { + "config": { + "name": "", + "request": {} + } + "testcases": { + "testcase1": { + "testcase": "/path/to/testcase", + "variables": {...}, + "parameters": {...} + }, + "testcase2": {} + } + } Returns: - dict: expected api definition if found. - - Raises: - exceptions.ApiNotFound: api not found + dict: loaded testsuite content + { + "config": {}, + "testcases": [testcase1, testcase2] + } """ - try: - block = tests_def_mapping["api"][name] - # NOTICE: avoid project_mapping been changed during iteration. - return utils.deepcopy_dict(block) - except KeyError: - raise exceptions.ApiNotFound("{} not found!".format(name)) + testcases = raw_testsuite["testcases"] + for name, raw_testcase in testcases.items(): + __extend_with_testcase_ref(raw_testcase) + raw_testcase.setdefault("name", name) + + return raw_testsuite + + +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) + loaded_content = None + + if isinstance(raw_content, dict): + + if "testcases" in raw_content: + # file_type: testsuite + # TODO: add json schema validation for testsuite + loaded_content = load_testsuite(raw_content) + loaded_content["path"] = path + loaded_content["type"] = "testsuite" + elif "request" in raw_content: + # file_type: api + # TODO: add json schema validation for api + loaded_content = raw_content + loaded_content["path"] = path + loaded_content["type"] = "api" + else: + # invalid format + logger.log_warning("Invalid test file format: {}".format(path)) + + elif isinstance(raw_content, list) and len(raw_content) > 0: + # file_type: testcase + # TODO: add json schema validation for testcase + loaded_content = load_testcase(raw_content) + loaded_content["path"] = path + loaded_content["type"] = "testcase" + + else: + # invalid format + logger.log_warning("Invalid test file format: {}".format(path)) + + return loaded_content def load_folder_content(folder_path): @@ -623,7 +719,7 @@ def load_tests(path, dot_env_path=None): "path": "testcase1_path", "variables": [], # optional }, - "tests": [ + "teststeps": [ # test data structure { 'name': 'test desc1', @@ -635,7 +731,17 @@ def load_tests(path, dot_env_path=None): test_dict_2 # another test dict ] }, - testcase_dict_2 # another testcase dict + testcase_2_dict # another testcase dict + ], + "testsuites": [ + { # testsuite data structure + "config": {}, + "testcases": { + "testcase1": {}, + "testcase2": {}, + } + }, + testsuite_2_dict ] } @@ -653,33 +759,23 @@ def load_tests(path, dot_env_path=None): "project_mapping": project_mapping } - def load_test_file(path): - raw_testcase = load_file(path) - - try: - testcase = load_testcase(raw_testcase) - testcase["config"]["path"] = path - except exceptions.FileFormatError: - testcase = {} - - return testcase - - testcases_list = [] + def __load_file_content(path): + loaded_content = load_test_file(path) + if not loaded_content: + pass + elif loaded_content["type"] == "testsuite": + tests_mapping.setdefault("testsuites", []).append(loaded_content) + elif loaded_content["type"] == "testcase": + tests_mapping.setdefault("testcases", []).append(loaded_content) + elif loaded_content["type"] == "api": + tests_mapping.setdefault("api", []).append(loaded_content) if os.path.isdir(path): files_list = load_folder_files(path) for path in files_list: - testcase = load_test_file(path) - if not testcase: - continue - testcases_list.append(testcase) + __load_file_content(path) elif os.path.isfile(path): - - testcase = load_test_file(path) - if testcase: - testcases_list.append(testcase) - - tests_mapping["testcases"] = testcases_list + __load_file_content(path) return tests_mapping diff --git a/httprunner/parser.py b/httprunner/parser.py index d835f94a..3a7f020f 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -676,7 +676,7 @@ def _extend_with_testcase(test_dict, testcase_def_dict): def __parse_config(config, project_mapping): - """ parse testcase config, include variables and name. + """ parse testcase/testsuite config, include variables and name. """ # get config variables raw_config_variables = config.pop("variables", {}) @@ -684,31 +684,29 @@ def __parse_config(config, project_mapping): override_variables = utils.deepcopy_dict(project_mapping.get("variables", {})) functions = project_mapping.get("functions", {}) - # override testcase config variables with passed in variables + # override config variables with passed in variables + raw_config_variables_mapping.update(override_variables) + + # parse config variables + parsed_config_variables = {} for key, value in raw_config_variables_mapping.items(): + try: + parsed_value = parse_data( + value, + raw_config_variables_mapping, + functions + ) + except exceptions.VariableNotFound: + pass + parsed_config_variables[key] = parsed_value - if key in override_variables: - # passed in - continue - else: - # config variables - try: - parsed_value = parse_data( - value, - override_variables, - functions - ) - except exceptions.VariableNotFound: - pass - override_variables[key] = parsed_value - - if override_variables: - config["variables"] = override_variables + if parsed_config_variables: + config["variables"] = parsed_config_variables # parse config name config["name"] = parse_data( config.get("name", ""), - override_variables, + parsed_config_variables, functions ) @@ -716,17 +714,17 @@ def __parse_config(config, project_mapping): if "base_url" in config: config["base_url"] = parse_data( config["base_url"], - override_variables, + parsed_config_variables, functions ) -def __parse_tests(tests, config, project_mapping): +def __parse_testcase_tests(tests, config, project_mapping): """ override tests with testcase config variables, base_url and verify. test maybe nested testcase. variables priority: - testsuite config > testsuite test > testcase config > testcase test > api + testcase config > testcase test > testcase_def config > testcase_def test > api base_url/verify priority: testcase test > testcase config > testsuite test > testsuite config > api @@ -734,9 +732,7 @@ def __parse_tests(tests, config, project_mapping): Args: tests (list): config (dict): - - Returns: - list: overrided tests + project_mapping (dict): """ config_variables = config.pop("variables", {}) @@ -752,51 +748,34 @@ def __parse_tests(tests, config, project_mapping): test_dict.setdefault("verify", config_verify) + # 1, testcase config => testcase tests + # override test_dict variables + test_dict["variables"] = utils.extend_variables( + test_dict.pop("variables", {}), + config_variables + ) + + # parse test_dict name + try: + test_dict["name"] = parse_data( + test_dict.pop("name", ""), + test_dict["variables"], + functions + ) + except exceptions.VariableNotFound: + pass + if "testcase_def" in test_dict: # test_dict is nested testcase - # 1, testsuite config => testsuite tests - # override test_dict variables - test_dict["variables"] = utils.extend_variables( - test_dict.pop("variables", {}), - config_variables - ) - - # parse test_dict name - try: - test_dict["name"] = parse_data( - test_dict.pop("name", ""), - test_dict["variables"], - functions - ) - except exceptions.VariableNotFound: - pass - - # 2, testsuite test_dict => testcase config + # 2, testcase test_dict => testcase_def config testcase_def = test_dict.pop("testcase_def") _extend_with_testcase(test_dict, testcase_def) - # 3, testcase config => testcase test_dict + # 3, testcase_def config => testcase_def test_dict _parse_testcase(test_dict, project_mapping) else: - # 1, config => tests - # override test_dict variables - test_dict["variables"] = utils.extend_variables( - test_dict.pop("variables", {}), - config_variables - ) - - # parse test_dict name - try: - test_dict["name"] = parse_data( - test_dict.pop("name", ""), - test_dict["variables"], - functions - ) - except exceptions.VariableNotFound: - pass - if "api_def" in test_dict: # test_dict has API reference # 2, test_dict => api @@ -804,11 +783,14 @@ def __parse_tests(tests, config, project_mapping): _extend_with_api(test_dict, api_def_dict) if test_dict.get("base_url"): + # parse base_url base_url = parse_data( test_dict.pop("base_url"), test_dict["variables"], functions ) + + # build path with base_url try: request_url = parse_data( test_dict["request"]["url"], @@ -817,7 +799,7 @@ def __parse_tests(tests, config, project_mapping): ) except exceptions.VariableNotFound: """ variable in current url maybe extracted from former api - "tests": [ + "teststeps": [ { "request": {}, "extract": { @@ -840,12 +822,125 @@ def __parse_tests(tests, config, project_mapping): def _parse_testcase(testcase, project_mapping): + """ parse testcase + + Args: + testcase (dict): + { + "config": {}, + "teststeps": [] + } + + """ + testcase.setdefault("config", {}) __parse_config(testcase["config"], project_mapping) - __parse_tests(testcase["tests"], testcase["config"], project_mapping) + __parse_testcase_tests(testcase["teststeps"], testcase["config"], project_mapping) + + +def __get_parsed_testsuite_testcases(testcases, testsuite_config, project_mapping): + """ override testscases with testsuite config variables, base_url and verify. + + variables priority: + testsuite config > testcase config > testcase_def config > testcase_def tests > api + + Args: + testcases (dict): + { + "testcase1 name": { + "testcase": "testcases/create_and_check.yml", + "weight": 2, + "variables": { + "uid": 1000 + }, + "parameters": { + "uid": [100, 101, 102] + }, + "testcase_def": { + "config": {}, + "teststeps": [] + } + }, + "testcase2 name": {} + } + testsuite_config (dict): + { + "name": "testsuite name", + "variables": { + "device_sn": "${gen_random_string(15)}" + }, + "base_url": "http://127.0.0.1:5000" + } + project_mapping (dict): + { + "env": {}, + "functions": {} + } + + """ + testsuite_config_variables = testsuite_config.get("variables", {}) + functions = project_mapping.get("functions", {}) + parsed_testcase_list = [] + + for testcase_name, testcase in testcases.items(): + + # TODO: add parameterize + # parameters = testcase.get("parameters") + + parsed_testcase = testcase.pop("testcase_def") + parsed_testcase.setdefault("config", {}) + parsed_testcase["path"] = testcase["testcase"] + parsed_testcase["config"]["name"] = testcase_name + + # 1, testsuite config => testcase config + # override test_dict variables + testcase_config_variables = utils.extend_variables( + testcase.pop("variables", {}), + testsuite_config_variables + ) + + # 2, testcase config > testcase_def config + # override testcase_def config variables + parsed_testcase_config_variables = utils.extend_variables( + parsed_testcase["config"].pop("variables", {}), + testcase_config_variables + ) + + # parse config variables + parsed_config_variables = {} + for key, value in parsed_testcase_config_variables.items(): + try: + parsed_value = parse_data( + value, + parsed_testcase_config_variables, + functions + ) + except exceptions.VariableNotFound: + pass + parsed_config_variables[key] = parsed_value + + if parsed_config_variables: + parsed_testcase["config"]["variables"] = parsed_config_variables + + _parse_testcase(parsed_testcase, project_mapping) + parsed_testcase_list.append(parsed_testcase) + + return parsed_testcase_list + + +def _parse_testsuite(testsuite, project_mapping): + testsuite.setdefault("config", {}) + __parse_config(testsuite["config"], project_mapping) + parsed_testcase_list = __get_parsed_testsuite_testcases( + testsuite["testcases"], + testsuite["config"], + project_mapping + ) + return parsed_testcase_list def parse_tests(tests_mapping): - """ parse testcases configs, including variables/name/request. + """ parse tests and load to parsed testcases + tests include api, testcases and testsuites. Args: tests_mapping (dict): project info and testcases list. @@ -857,14 +952,34 @@ def parse_tests(tests_mapping): "variables": {}, # optional, priority 1 "env": {} }, + "testsuites": [ + { # testsuite data structure + "config": {}, + "testcases": { + "testcase1 name": { + "variables": { + "uid": 1000 + }, + "parameters": { + "uid": [100, 101, 102] + }, + "testcase_def": { + "config": {}, + "teststeps": [] + } + }, + "testcase2 name": {} + } + } + ], "testcases": [ { # testcase data structure "config": { "name": "desc1", "path": "testcase1_path", - "variables": [], # optional, priority 2 + "variables": {}, # optional, priority 2 }, - "tests": [ + "teststeps": [ # test data structure { 'name': 'test step desc1', @@ -880,12 +995,41 @@ def parse_tests(tests_mapping): ] }, testcase_dict_2 # another testcase dict - ] + ], + "api": { + "variables": {}, + "request": {} + } } """ project_mapping = tests_mapping.get("project_mapping", {}) + parsed_tests_mapping = { + "project_mapping": project_mapping, + "testcases": [] + } - for testcase in tests_mapping["testcases"]: - testcase.setdefault("config", {}) - _parse_testcase(testcase, project_mapping) + for test_type in tests_mapping: + + if test_type == "testsuites": + # load testcases of testsuite + testsuites = tests_mapping["testsuites"] + for testsuite in testsuites: + parsed_testcases = _parse_testsuite(testsuite, project_mapping) + for parsed_testcase in parsed_testcases: + parsed_tests_mapping["testcases"].append(parsed_testcase) + + elif test_type == "testcases": + for testcase in tests_mapping["testcases"]: + _parse_testcase(testcase, project_mapping) + parsed_tests_mapping["testcases"].append(testcase) + + elif test_type == "api": + # encapsulate api as a testcase + testcase = { + "teststeps": tests_mapping["api"] + } + _parse_testcase(testcase, project_mapping) + parsed_tests_mapping["testcases"].append(testcase) + + return parsed_tests_mapping diff --git a/httprunner/runner.py b/httprunner/runner.py index 68bbcb65..1c48583b 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -282,7 +282,7 @@ class Runner(object): http_client_session = self.http_client_session.__class__(base_url) test_runner = Runner(config, self.functions, http_client_session) - tests = testcase_dict.get("tests", []) + tests = testcase_dict.get("teststeps", []) try: for index, test_dict in enumerate(tests): @@ -317,7 +317,7 @@ class Runner(object): # nested testcase { "config": {...}, - "tests": [ + "teststeps": [ {...}, {...} ] diff --git a/httprunner/utils.py b/httprunner/utils.py index 586e57fc..b0d8bda5 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -689,11 +689,17 @@ def dump_tests(tests_mapping, tag_name): tests_to_dump["project_mapping"][key] = project_mapping[key] continue + # remove functions in order to dump if project_mapping["functions"]: debugtalk_py_path = os.path.join(pwd_dir_path, "debugtalk.py") tests_to_dump["project_mapping"]["debugtalk.py"] = debugtalk_py_path - tests_to_dump["testcases"] = tests_mapping["testcases"] + if "api" in tests_mapping: + tests_to_dump["api"] = tests_mapping["api"] + elif "testcases" in tests_mapping: + tests_to_dump["testcases"] = tests_mapping["testcases"] + elif "testsuites" in tests_mapping: + tests_to_dump["testsuites"] = tests_mapping["testsuites"] dump_json_file(tests_to_dump, pwd_dir_path, dump_file_name) diff --git a/httprunner/validator.py b/httprunner/validator.py index fcccc7c6..6e52c332 100644 --- a/httprunner/validator.py +++ b/httprunner/validator.py @@ -19,7 +19,7 @@ def is_testcase(data_structure): "variables": [], # optional "request": {} # optional }, - "tests": [ + "teststeps": [ test_dict1, { # test_dict2 'name': 'test step desc2', @@ -40,10 +40,10 @@ def is_testcase(data_structure): if not isinstance(data_structure, dict): return False - if "tests" not in data_structure: + if "teststeps" not in data_structure: return False - if not isinstance(data_structure["tests"], list): + if not isinstance(data_structure["teststeps"], list): return False return True @@ -67,7 +67,7 @@ def is_testcases(data_structure): "path": "testcase1_path", "variables": [], # optional }, - "tests": [ + "teststeps": [ # test data structure { 'name': 'test step desc1', diff --git a/tests/api/create_user.yml b/tests/api/create_user.yml index 2f7657b8..6e6ceebc 100644 --- a/tests/api/create_user.yml +++ b/tests/api/create_user.yml @@ -1,3 +1,4 @@ +name: create user variables: user_name: user0 user_password: "000000" diff --git a/tests/test_api.py b/tests/test_api.py index fac7f6d7..d490c818 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -29,7 +29,7 @@ class TestHttpRunner(ApiServerUnittest): }, 'variables': [] }, - 'tests': [ + "teststeps": [ { 'name': '/api/get-token', 'request': { @@ -119,7 +119,7 @@ class TestHttpRunner(ApiServerUnittest): }, 'variables': [] }, - "tests": [ + "teststeps": [ { "name": "post data", "request": { @@ -164,7 +164,7 @@ class TestHttpRunner(ApiServerUnittest): self.runner.run("tests/testsuites/create_users.yml") summary = self.runner.summary self.assertTrue(summary["success"]) - self.assertEqual(summary["stat"]["testsRun"], 2) + self.assertEqual(summary["stat"]["testsRun"], 8) def test_run_httprunner_with_hooks(self): testcase_file_path = os.path.join( @@ -180,7 +180,7 @@ class TestHttpRunner(ApiServerUnittest): testcases = [ { "config": {"name": "test teardown hooks"}, - "tests": [ + "teststeps": [ { "name": "test teardown hooks", "request": { @@ -220,7 +220,7 @@ class TestHttpRunner(ApiServerUnittest): "config": { "name": "test teardown hooks" }, - "tests": [ + "teststeps": [ { "name": "test teardown hooks", "request": { @@ -254,7 +254,7 @@ class TestHttpRunner(ApiServerUnittest): "config": { "name": "test teardown hooks" }, - "tests": [ + "teststeps": [ { "name": "test teardown hooks", "request": { @@ -361,11 +361,11 @@ class TestHttpRunner(ApiServerUnittest): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_parameters.yml') tests_mapping = loader.load_tests(testcase_file_path) - parser.parse_tests(tests_mapping) - test_suite = self.runner._add_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_suite = self.runner._add_tests(parsed_tests_mapping) self.assertEqual( - test_suite._tests[0].tests[0]['name'], + test_suite._tests[0].teststeps[0]['name'], 'get token with iOS/10.1 and test1' ) # TODO: add parameterize @@ -415,9 +415,9 @@ class TestApi(ApiServerUnittest): self.assertEqual(len(testcases), 1) testcase_config = testcases[0]["config"] self.assertEqual(testcase_config["name"], "setup and reset all.") - self.assertIn("path", testcase_config) + self.assertIn("path", testcases[0]) - testcase_tests = testcases[0]["tests"] + testcase_tests = testcases[0]["teststeps"] self.assertEqual(len(testcase_tests), 2) self.assertIn("api", testcase_tests[0]) self.assertEqual(testcase_tests[0]["name"], "get token (setup)") @@ -429,15 +429,15 @@ class TestApi(ApiServerUnittest): testcase_path = "tests/testcases/setup.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) - parsed_testcases = tests_mapping["testcases"] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + parsed_testcases = parsed_tests_mapping["testcases"] self.assertEqual(len(parsed_testcases), 1) self.assertNotIn("variables", parsed_testcases[0]["config"]) - self.assertEqual(len(parsed_testcases[0]["tests"]), 2) + self.assertEqual(len(parsed_testcases[0]["teststeps"]), 2) - test_dict1 = parsed_testcases[0]["tests"][0] + test_dict1 = parsed_testcases[0]["teststeps"][0] self.assertEqual(test_dict1["name"], "get token (setup)") self.assertNotIn("api_def", test_dict1) self.assertEqual(test_dict1["variables"]["device_sn"], "TESTCASE_SETUP_XXX") @@ -447,31 +447,31 @@ class TestApi(ApiServerUnittest): testcase_path = "tests/testcases/setup.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) runner = HttpRunner() - test_suite = runner._add_tests(tests_mapping) + test_suite = runner._add_tests(parsed_tests_mapping) self.assertEqual(len(test_suite._tests), 1) - tests = test_suite._tests[0].tests - self.assertEqual(tests[0]["name"], "get token (setup)") - self.assertEqual(tests[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX") - self.assertIn("api", tests[0]) + teststeps = test_suite._tests[0].teststeps + self.assertEqual(teststeps[0]["name"], "get token (setup)") + self.assertEqual(teststeps[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX") + self.assertIn("api", teststeps[0]) def test_testcase_simple_run_suite(self): testcase_path = "tests/testcases/setup.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) runner = HttpRunner() - test_suite = runner._add_tests(tests_mapping) + test_suite = runner._add_tests(parsed_tests_mapping) tests_results = runner._run_suite(test_suite) self.assertEqual(len(tests_results[0][1].records), 2) def test_testcase_complex_run_suite(self): testcase_path = "tests/testcases/create_and_check.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) runner = HttpRunner() - test_suite = runner._add_tests(tests_mapping) + test_suite = runner._add_tests(parsed_tests_mapping) tests_results = runner._run_suite(test_suite) self.assertEqual(len(tests_results[0][1].records), 4) @@ -495,74 +495,77 @@ class TestApi(ApiServerUnittest): self.assertIn("functions", project_mapping) self.assertIn("env", project_mapping) - testcases = tests_mapping["testcases"] - self.assertIsInstance(testcases, list) - self.assertEqual(len(testcases), 1) - testcase_config = testcases[0]["config"] - self.assertEqual(testcase_config["name"], "create users with uid") - self.assertIn("path", testcase_config) + testsuites = tests_mapping["testsuites"] + self.assertIsInstance(testsuites, list) + self.assertEqual(len(testsuites), 1) - testcase_tests = testcases[0]["tests"] - self.assertEqual(len(testcase_tests), 2) - self.assertIn("testcase_def", testcase_tests[0]) - self.assertEqual(testcase_tests[0]["name"], "create user 1000 and check result.") - self.assertIsInstance(testcase_tests[0]["testcase_def"], dict) - self.assertEqual(testcase_tests[0]["testcase_def"]["config"]["name"], "create user and check result.") - self.assertEqual(len(testcase_tests[0]["testcase_def"]["tests"]), 4) - self.assertEqual(testcase_tests[0]["testcase_def"]["tests"][0]["name"], "setup and reset all (override).") + self.assertIn("path", testsuites[0]) + testsuite_config = testsuites[0]["config"] + self.assertEqual(testsuite_config["name"], "create users with uid") + + testcases = testsuites[0]["testcases"] + self.assertEqual(len(testcases), 2) + self.assertIn("create user 1000 and check result.", testcases) + testcase_tests = testcases["create user 1000 and check result."] + self.assertIn("testcase_def", testcase_tests) + self.assertEqual(testcase_tests["name"], "create user 1000 and check result.") + self.assertIsInstance(testcase_tests["testcase_def"], dict) + self.assertEqual(testcase_tests["testcase_def"]["config"]["name"], "create user and check result.") + self.assertEqual(len(testcase_tests["testcase_def"]["teststeps"]), 4) + self.assertEqual(testcase_tests["testcase_def"]["teststeps"][0]["name"], "setup and reset all (override).") def test_testsuite_parser(self): testcase_path = "tests/testsuites/create_users.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) - parsed_testcases = tests_mapping["testcases"] - self.assertEqual(len(parsed_testcases), 1) - self.assertEqual(len(parsed_testcases[0]["tests"]), 2) + parsed_testcases = parsed_tests_mapping["testcases"] + self.assertEqual(len(parsed_testcases), 2) + self.assertEqual(len(parsed_testcases[0]["teststeps"]), 4) - testcase1 = parsed_testcases[0]["tests"][0] - self.assertEqual(testcase1["config"]["name"], "create user 1000 and check result.") + testcase1 = parsed_testcases[0]["teststeps"][0] + self.assertEqual(testcase1["config"]["name"], "setup and reset all (override).") self.assertNotIn("testcase_def", testcase1) - self.assertEqual(len(testcase1["tests"]), 4) + self.assertEqual(len(testcase1["teststeps"]), 2) self.assertEqual( - testcase1["tests"][0]["tests"][0]["request"]["url"], + testcase1["teststeps"][0]["request"]["url"], "http://127.0.0.1:5000/api/get-token" ) - self.assertEqual(len(testcase1["tests"][0]["tests"][0]["variables"]["device_sn"]), 15) + self.assertEqual(len(testcase1["teststeps"][0]["variables"]["device_sn"]), 15) def test_testsuite_add_tests(self): testcase_path = "tests/testsuites/create_users.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) runner = HttpRunner() - test_suite = runner._add_tests(tests_mapping) + test_suite = runner._add_tests(parsed_tests_mapping) - self.assertEqual(len(test_suite._tests), 1) - tests = test_suite._tests[0].tests - self.assertEqual(tests[0]["config"]["name"], "create user 1000 and check result.") + self.assertEqual(len(test_suite._tests), 2) + tests = test_suite._tests[0].teststeps + self.assertEqual(tests[0]["config"]["name"], "setup and reset all (override).") def test_testsuite_run_suite(self): testcase_path = "tests/testsuites/create_users.yml" tests_mapping = loader.load_tests(testcase_path) - parser.parse_tests(tests_mapping) + parsed_tests_mapping = parser.parse_tests(tests_mapping) runner = HttpRunner() - test_suite = runner._add_tests(tests_mapping) + test_suite = runner._add_tests(parsed_tests_mapping) tests_results = runner._run_suite(test_suite) - self.assertEqual(len(tests_results[0][1].records), 2) + self.assertEqual(len(tests_results[0][1].records), 4) results = tests_results[0][1] self.assertEqual( results.records[0]["name"], - "create user 1000 and check result." + "setup and reset all (override)." ) - self.assertEqual( + self.assertIn( results.records[1]["name"], - "create user 1001 and check result." + ["make sure user 1000 does not exist", "make sure user 1001 does not exist"] ) diff --git a/tests/test_loader.py b/tests/test_loader.py index fa7be975..c85fd39d 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -186,6 +186,13 @@ class TestFileLoader(unittest.TestCase): os.path.join(os.getcwd(), "tests", "debugtalk.py") ) + def test_load_folder_content(self): + path = os.path.join(os.getcwd(), "tests", "api") + items_mapping = loader.load_folder_content(path) + file_path = os.path.join(os.getcwd(), "tests", "api", "basic.yml") + self.assertIn(file_path, items_mapping) + self.assertIsInstance(items_mapping[file_path], list) + class TestModuleLoader(unittest.TestCase): @@ -235,7 +242,111 @@ class TestModuleLoader(unittest.TestCase): ) self.assertEqual(debugtalk_functions, {}) - def test_load_tests(self): + +class TestSuiteLoader(unittest.TestCase): + + @classmethod + def setUpClass(cls): + loader.load_project_tests(os.path.join(os.getcwd(), "tests")) + cls.project_mapping = loader.project_mapping + cls.tests_def_mapping = loader.tests_def_mapping + + def test_load_teststep_api(self): + raw_test = { + "name": "create user (override).", + "api": "api/create_user.yml", + "variables": [ + {"uid": "999"} + ] + } + teststep = loader.load_teststep(raw_test) + self.assertEqual( + "create user (override).", + teststep["name"] + ) + self.assertIn("api_def", teststep) + api_def = teststep["api_def"] + self.assertEqual(api_def["name"], "create user") + self.assertEqual(api_def["request"]["url"], "/api/users/$uid") + + def test_load_teststep_testcase(self): + raw_test = { + "name": "setup and reset all (override).", + "testcase": "testcases/setup.yml", + "variables": [ + {"device_sn": "$device_sn"} + ], + "output": ["token", "device_sn"] + } + testcase = loader.load_teststep(raw_test) + self.assertEqual( + "setup and reset all (override).", + testcase["name"] + ) + tests = testcase["testcase_def"]["teststeps"] + self.assertEqual(len(tests), 2) + self.assertEqual(tests[0]["name"], "get token (setup)") + self.assertEqual(tests[1]["name"], "reset all users") + + def test_load_test_file_api(self): + loaded_content = loader.load_test_file("tests/api/create_user.yml") + self.assertEqual(loaded_content["type"], "api") + self.assertIn("path", loaded_content) + self.assertIn("request", loaded_content) + self.assertEqual(loaded_content["request"]["url"], "/api/users/$uid") + + def test_load_test_file_testcase(self): + loaded_content = loader.load_test_file("tests/testcases/setup.yml") + self.assertEqual(loaded_content["type"], "testcase") + self.assertIn("path", loaded_content) + self.assertIn("config", loaded_content) + self.assertEqual(loaded_content["config"]["name"], "setup and reset all.") + self.assertIn("teststeps", loaded_content) + self.assertEqual(len(loaded_content["teststeps"]), 2) + + def test_load_test_file_testsuite(self): + loaded_content = loader.load_test_file("tests/testsuites/create_users.yml") + self.assertEqual(loaded_content["type"], "testsuite") + + testcases = loaded_content["testcases"] + self.assertEqual(len(testcases), 2) + self.assertIn('create user 1000 and check result.', testcases) + self.assertIn('testcase_def', testcases["create user 1000 and check result."]) + self.assertEqual( + testcases["create user 1000 and check result."]["testcase_def"]["config"]["name"], + "create user and check result." + ) + + def test_load_tests_api_file(self): + path = os.path.join( + os.getcwd(), 'tests/api/create_user.yml') + tests_mapping = loader.load_tests(path) + project_mapping = tests_mapping["project_mapping"] + api_list = tests_mapping["api"] + self.assertEqual(len(api_list), 1) + self.assertEqual(api_list[0]["request"]["url"], "/api/users/$uid") + + def test_load_tests_testcase_file(self): + # absolute file path + path = os.path.join( + os.getcwd(), 'tests/data/demo_testcase_hardcode.json') + tests_mapping = loader.load_tests(path) + project_mapping = tests_mapping["project_mapping"] + testcases_list = tests_mapping["testcases"] + self.assertEqual(len(testcases_list), 1) + self.assertEqual(len(testcases_list[0]["teststeps"]), 3) + self.assertIn("get_sign", project_mapping["functions"]) + + # relative file path + path = 'tests/data/demo_testcase_hardcode.yml' + tests_mapping = loader.load_tests(path) + project_mapping = tests_mapping["project_mapping"] + testcases_list = tests_mapping["testcases"] + self.assertEqual(len(testcases_list), 1) + self.assertEqual(len(testcases_list[0]["teststeps"]), 3) + self.assertIn("get_sign", project_mapping["functions"]) + + def test_load_tests_testcase_file_2(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testcase.yml') tests_mapping = loader.load_tests(testcase_file_path) @@ -255,83 +366,52 @@ class TestModuleLoader(unittest.TestCase): "${ENV(PROJECT_KEY)}" ) - -class TestSuiteLoader(unittest.TestCase): - - @classmethod - def setUpClass(cls): - loader.load_project_tests(os.path.join(os.getcwd(), "tests")) - cls.project_mapping = loader.project_mapping - cls.tests_def_mapping = loader.tests_def_mapping - - def test_load_test_testcase(self): - raw_test = { - "name": "setup and reset all (override).", - "testcase": "testcases/setup.yml", - "variables": [ - {"device_sn": "$device_sn"} - ], - "output": ["token", "device_sn"] - } - testcase = loader.load_test(raw_test) - self.assertEqual( - "setup and reset all (override).", - testcase["name"] - ) - tests = testcase["testcase_def"]["tests"] - self.assertEqual(len(tests), 2) - self.assertEqual(tests[0]["name"], "get token (setup)") - self.assertEqual(tests[1]["name"], "reset all users") - - def test_load_testcase(self): - raw_testcase = loader.load_file("tests/testsuites/create_users.yml") - testcase = loader.load_testcase(raw_testcase) - self.assertEqual(testcase["config"]["name"], "create users with uid") - self.assertIn("device_sn", testcase["config"]["variables"]) - self.assertEqual(len(testcase["tests"]), 2) - self.assertEqual(testcase["tests"][0]["name"], "create user 1000 and check result.") - self.assertEqual(testcase["tests"][0]["testcase_def"]["config"]["name"], "create user and check result.") - - def test_load_testcases_by_path_files(self): - # absolute file path + def test_load_tests_testcase_file_with_api_ref(self): path = os.path.join( - os.getcwd(), 'tests/data/demo_testcase_hardcode.json') + os.getcwd(), 'tests/data/demo_testcase_layer.yml') tests_mapping = loader.load_tests(path) project_mapping = tests_mapping["project_mapping"] testcases_list = tests_mapping["testcases"] - self.assertEqual(len(testcases_list), 1) - self.assertEqual(len(testcases_list[0]["tests"]), 3) - self.assertIn("get_sign", project_mapping["functions"]) + self.assertIn('device_sn', testcases_list[0]["config"]["variables"]) + self.assertIn("gen_md5", project_mapping["functions"]) + self.assertIn("base_url", testcases_list[0]["config"]) + test_dict0 = testcases_list[0]["teststeps"][0] + self.assertEqual( + "get token with $user_agent, $app_version", + test_dict0["name"] + ) + self.assertIn("/api/get-token", test_dict0["api_def"]["request"]["url"]) + self.assertIn( + {'eq': ['status_code', 200]}, + test_dict0["validate"] + ) - # relative file path - path = 'tests/data/demo_testcase_hardcode.yml' + def test_load_tests_testsuite_file_with_testcase_ref(self): + path = os.path.join( + os.getcwd(), 'tests/testsuites/create_users.yml') tests_mapping = loader.load_tests(path) project_mapping = tests_mapping["project_mapping"] - testcases_list = tests_mapping["testcases"] - self.assertEqual(len(testcases_list), 1) - self.assertEqual(len(testcases_list[0]["tests"]), 3) - self.assertIn("get_sign", project_mapping["functions"]) + testsuites_list = tests_mapping["testsuites"] - # TODO: list/set container with file(s) - # path = [ - # os.path.join(os.getcwd(), 'tests/data/demo_testcase_hardcode.json'), - # 'tests/data/demo_testcase_hardcode.yml' - # ] - # testcases_list = loader.load_tests(path) - # self.assertEqual(len(testcases_list), 2) - # self.assertEqual(len(testcases_list[0]["tests"]), 3) - # self.assertEqual(len(testcases_list[1]["tests"]), 3) - # testcases_list.extend(testcases_list) - # self.assertEqual(len(testcases_list), 4) + self.assertEqual( + "create users with uid", + testsuites_list[0]["config"]["name"] + ) + self.assertEqual( + {'device_sn': '${gen_random_string(15)}'}, + testsuites_list[0]["config"]["variables"] + ) + self.assertIn( + "create user 1000 and check result.", + testsuites_list[0]["testcases"] + ) - # for testcase in testcases_list: - # for test_dict in testcase["tests"]: - # self.assertIn('name', test_dict) - # self.assertIn('request', test_dict) - # self.assertIn('url', test_dict['request']) - # self.assertIn('method', test_dict['request']) + self.assertEqual( + testsuites_list[0]["testcases"]["create user 1000 and check result."]["testcase_def"]["config"]["name"], + "create user and check result." + ) - def test_load_testcases_by_path_folder(self): + def test_load_tests_folder_path(self): # absolute folder path path = os.path.join(os.getcwd(), 'tests/data') tests_mapping = loader.load_tests(path) @@ -344,16 +424,7 @@ class TestSuiteLoader(unittest.TestCase): testcase_list_2 = tests_mapping["testcases"] self.assertEqual(len(testcase_list_1), len(testcase_list_2)) - # TODO: list/set container with file(s) - # path = [ - # os.path.join(os.getcwd(), 'tests/data'), - # 'tests/data/' - # ] - # tests_mapping = loader.load_tests(path) - # testcase_list_3 = tests_mapping["testcases"] - # self.assertEqual(len(testcase_list_3), 2 * len(testcase_list_1)) - - def test_load_testcases_by_path_not_exist(self): + def test_load_tests_path_not_exist(self): # absolute folder path path = os.path.join(os.getcwd(), 'tests/data_not_exist') with self.assertRaises(exceptions.FileNotFound): @@ -364,76 +435,6 @@ class TestSuiteLoader(unittest.TestCase): with self.assertRaises(exceptions.FileNotFound): loader.load_tests(path) - # TODO: list/set container with file(s) - # path = [ - # os.path.join(os.getcwd(), 'tests/data_not_exist'), - # 'tests/data_not_exist/' - # ] - # with self.assertRaises(exceptions.FileNotFound): - # loader.load_tests(path) - - def test_load_testcases_with_api_ref(self): - path = os.path.join( - os.getcwd(), 'tests/data/demo_testcase_layer.yml') - tests_mapping = loader.load_tests(path) - project_mapping = tests_mapping["project_mapping"] - testcases_list = tests_mapping["testcases"] - self.assertIn('device_sn', testcases_list[0]["config"]["variables"]) - self.assertIn("gen_md5", project_mapping["functions"]) - self.assertIn("base_url", testcases_list[0]["config"]) - test_dict0 = testcases_list[0]["tests"][0] - self.assertEqual( - "get token with $user_agent, $app_version", - test_dict0["name"] - ) - self.assertIn("/api/get-token", test_dict0["api_def"]["request"]["url"]) - self.assertIn( - {'eq': ['status_code', 200]}, - test_dict0["validate"] - ) - - def test_load_testcases_with_testcase_ref(self): - path = os.path.join( - os.getcwd(), 'tests/testsuites/create_users.yml') - tests_mapping = loader.load_tests(path) - project_mapping = tests_mapping["project_mapping"] - testcases_list = tests_mapping["testcases"] - - self.assertEqual( - "create users with uid", - testcases_list[0]["config"]["name"] - ) - self.assertEqual( - {'device_sn': '${gen_random_string(15)}'}, - testcases_list[0]["config"]["variables"] - ) - testcase0 = testcases_list[0]["tests"][0] - self.assertEqual( - "create user 1000 and check result.", - testcase0["name"] - ) - self.assertEqual( - "create user and check result.", - testcase0["testcase_def"]["config"]["name"] - ) - - testcase1 = testcases_list[0]["tests"][1] - self.assertEqual( - "create user 1001 and check result.", - testcase1["name"] - ) - self.assertEqual( - {'uid': 1001}, - testcase1["variables"] - ) - - def test_load_folder_content(self): - path = os.path.join(os.getcwd(), "tests", "api") - items_mapping = loader.load_folder_content(path) - file_path = os.path.join(os.getcwd(), "tests", "api", "basic.yml") - self.assertIn(file_path, items_mapping) - self.assertIsInstance(items_mapping[file_path], list) - def test_load_api_folder(self): path = os.path.join(os.getcwd(), "tests", "api") api_definition_mapping = loader.load_api_folder(path) diff --git a/tests/test_parser.py b/tests/test_parser.py index 3b7f613b..b100991a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -429,7 +429,7 @@ class TestParser(unittest.TestCase): 3 * 2 * 3 ) - def test_parse_tests(self): + def test_parse_tests_testcase(self): testcase_file_path = os.path.join( os.getcwd(), 'tests/data/demo_testcase.yml') tests_mapping = loader.load_tests(testcase_file_path) @@ -442,10 +442,10 @@ class TestParser(unittest.TestCase): testcases[0]["config"]["variables"]["PROJECT_KEY"], "${ENV(PROJECT_KEY)}" ) - parser.parse_tests(tests_mapping) - parsed_testcases = tests_mapping["testcases"] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + parsed_testcases = parsed_tests_mapping["testcases"] self.assertIsInstance(parsed_testcases, list) - test_dict1 = parsed_testcases[0]["tests"][0] + test_dict1 = parsed_testcases[0]["teststeps"][0] self.assertEqual(test_dict1["variables"]["var_c"], 3) self.assertEqual(test_dict1["variables"]["PROJECT_KEY"], "ABCDEFGH") # TODO: parameters @@ -456,14 +456,14 @@ class TestParser(unittest.TestCase): tests_mapping = { 'testcases': [ { - 'config': { + "config": { 'name': '', 'variables': [ {"password": "123456"}, {"creator": "user_test_001"} ] }, - 'tests': [ + "teststeps": [ { 'name': 'testcase1', "variables": [ @@ -476,8 +476,8 @@ class TestParser(unittest.TestCase): } ] } - parser.parse_tests(tests_mapping) - test_dict1_variables = tests_mapping["testcases"][0]["tests"][0]["variables"] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict1_variables = parsed_tests_mapping["testcases"][0]["teststeps"][0]["variables"] self.assertEqual(test_dict1_variables["creator"], "user_test_001") self.assertEqual(test_dict1_variables["username"], "$creator") @@ -487,7 +487,7 @@ class TestParser(unittest.TestCase): tests_mapping = { 'testcases': [ { - 'config': { + "config": { 'name': '', "base_url": "$host", 'variables': { @@ -495,7 +495,7 @@ class TestParser(unittest.TestCase): }, "verify": False }, - 'tests': [ + "teststeps": [ { 'name': 'testcase1', "base_url": "https://httprunner.org", @@ -505,8 +505,8 @@ class TestParser(unittest.TestCase): } ] } - parser.parse_tests(tests_mapping) - test_dict = tests_mapping["testcases"][0]["tests"][0] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") self.assertEqual(test_dict["request"]["verify"], True) @@ -514,14 +514,14 @@ class TestParser(unittest.TestCase): tests_mapping = { 'testcases': [ { - 'config': { + "config": { 'name': '', "base_url": "$host1", 'variables': { "host1": "https://debugtalk" } }, - 'tests': [ + "teststeps": [ { 'name': 'testcase1', "variables": { @@ -533,22 +533,22 @@ class TestParser(unittest.TestCase): } ] } - parser.parse_tests(tests_mapping) - test_dict = tests_mapping["testcases"][0]["tests"][0] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") def test_parse_tests_base_url_test_dict(self): tests_mapping = { 'testcases': [ { - 'config': { + "config": { 'name': '', "base_url": "$host1", 'variables': { "host1": "https://debugtalk" } }, - 'tests': [ + "teststeps": [ { 'name': 'testcase1', "base_url": "$host2", @@ -561,8 +561,8 @@ class TestParser(unittest.TestCase): } ] } - parser.parse_tests(tests_mapping) - test_dict = tests_mapping["testcases"][0]["tests"][0] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") def test_parse_tests_base_url_teststep_empty(self): @@ -571,7 +571,7 @@ class TestParser(unittest.TestCase): tests_mapping = { 'testcases': [ { - 'config': { + "config": { 'name': '', "base_url": "$host", 'variables': { @@ -579,7 +579,7 @@ class TestParser(unittest.TestCase): }, "verify": False }, - 'tests': [ + "teststeps": [ { 'name': 'testcase1', "base_url": "", @@ -589,8 +589,8 @@ class TestParser(unittest.TestCase): } ] } - parser.parse_tests(tests_mapping) - test_dict = tests_mapping["testcases"][0]["tests"][0] + parsed_tests_mapping = parser.parse_tests(tests_mapping) + test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0] self.assertEqual(test_dict["request"]["url"], "https://debugtalk/api1") self.assertEqual(test_dict["request"]["verify"], True) @@ -626,7 +626,7 @@ class TestParser(unittest.TestCase): "base_url": "https://debugtalk.com", "api": "get_token", } - api_def_dict = loader.load_test(raw_testinfo) + api_def_dict = loader.load_teststep(raw_testinfo) test_block = { "name": "override block", "times": 3, diff --git a/tests/test_runner.py b/tests/test_runner.py index ce87b5e2..5917205f 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -244,7 +244,7 @@ class TestRunner(ApiServerUnittest): tests_mapping = loader.load_tests(testcase_file_path) testcase = tests_mapping["testcases"][0] config_dict_headers = testcase["config"]["request"]["headers"] - test_dict_headers = testcase["tests"][0]["request"]["headers"] + test_dict_headers = testcase["teststeps"][0]["request"]["headers"] headers = deep_update_dict( config_dict_headers, test_dict_headers diff --git a/tests/test_validator.py b/tests/test_validator.py index 1797b0cb..64e97b2d 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -24,7 +24,7 @@ class TestValidator(unittest.TestCase): "path": "testcase1_path", "variables": [], # optional }, - "tests": [ + "teststeps": [ # test data structure { 'name': 'test step desc1', diff --git a/tests/testsuites/create_users.yml b/tests/testsuites/create_users.yml index 9234b328..8f772467 100644 --- a/tests/testsuites/create_users.yml +++ b/tests/testsuites/create_users.yml @@ -1,17 +1,18 @@ -- config: +config: name: create users with uid variables: device_sn: ${gen_random_string(15)} base_url: "http://127.0.0.1:5000" -- test: - name: create user 1000 and check result. - testcase: testcases/create_and_check.yml - variables: - uid: 1000 +testcases: + create user 1000 and check result.: + testcase: testcases/create_and_check.yml + weight: 2 + variables: + uid: 1000 -- test: - name: create user 1001 and check result. - testcase: testcases/create_and_check.yml - variables: - uid: 1001 + create user 1001 and check result.: + testcase: testcases/create_and_check.yml + weight: 23423 + variables: + uid: 1001