diff --git a/ate/__init__.py b/ate/__init__.py index 19442947..7320e64e 100644 --- a/ate/__init__.py +++ b/ate/__init__.py @@ -1 +1 @@ -__version__ = '0.7.0' \ No newline at end of file +__version__ = '0.7.1' \ No newline at end of file diff --git a/ate/testcase.py b/ate/testcase.py index 17337ff0..b8fbfccb 100644 --- a/ate/testcase.py +++ b/ate/testcase.py @@ -149,9 +149,72 @@ def update_test_info(test_dict, dir_path): api_call = test_dict["api"] function_meta = parse_function(api_call) func_name = function_meta["func_name"] + api_call_args = function_meta["args"] api_info = get_api_definition(func_name, dir_path) + api_def_args = api_info.get("function_meta").get("args", []) + + if len(api_call_args) != len(api_def_args): + raise exception.ParamsError("api call args invalid!") + + args_mapping = {} + for index, item in enumerate(api_def_args): + if api_call_args[index] == item: + continue + + args_mapping[item] = api_call_args[index] + + if args_mapping: + api_info = substitute_variables_with_mapping(api_info, args_mapping) + test_dict.update(api_info) +def substitute_variables_with_mapping(content, mapping): + """ substitute variables in content with mapping + e.g. + @params + content = { + 'request': { + 'url': '/api/users/$uid', + 'headers': {'token': '$token'} + } + } + mapping = {"$uid": 1000} + @return + { + 'request': { + 'url': '/api/users/1000', + 'headers': {'token': '$token'} + } + } + """ + if isinstance(content, (list, tuple)): + return [ + substitute_variables_with_mapping(item, mapping) + for item in content + ] + + if isinstance(content, dict): + substituted_data = {} + for key, value in content.items(): + eval_key = substitute_variables_with_mapping(key, mapping) + eval_value = substitute_variables_with_mapping(value, mapping) + substituted_data[eval_key] = eval_value + + return substituted_data + + if isinstance(content, (int, utils.long_type, float, complex)): + return content + + # content is in string format here + for var, value in mapping.items(): + if content == var: + # content is a variable + content = value + else: + content = content.replace(var, str(value)) + + return content + def get_api_definition(name, dir_path): """ get expected api from dir_path upward recursively @param diff --git a/tests/data/api.yml b/tests/data/api.yml index 0143e550..1e641a54 100644 --- a/tests/data/api.yml +++ b/tests/data/api.yml @@ -1,5 +1,5 @@ - api: - def: get_token($user_name, $device_sn, $os_platform, $app_version) + def: get_token($user_agent, $device_sn, $os_platform, $app_version) request: url: /api/get-token method: POST diff --git a/tests/data/demo_testset_layer.yml b/tests/data/demo_testset_layer.yml index 0ed32bc3..ed107f93 100644 --- a/tests/data/demo_testset_layer.yml +++ b/tests/data/demo_testset_layer.yml @@ -20,10 +20,9 @@ - test: name: create user which does not exist variable_binds: - - uid: 1000 - user_name: "user1" - user_password: "123456" - api: create_user($uid, $user_name, $user_password, $token) + api: create_user(1000, $user_name, $user_password, $token) validators: - {"check": "status_code", "comparator": "eq", "expected": 201} - {"check": "content.success", "comparator": "eq", "expected": true} @@ -31,10 +30,9 @@ - test: name: create user which does not exist variable_binds: - - uid: 1000 - user_name: "user1" - user_password: "123456" - api: create_user($uid, $user_name, $user_password, $token) + api: create_user(1000, $user_name, $user_password, $token) validators: - {"check": "status_code", "comparator": "eq", "expected": 500} - {"check": "content.success", "comparator": "eq", "expected": false} diff --git a/tests/test_testcase.py b/tests/test_testcase.py index 674abed0..0a19345e 100644 --- a/tests/test_testcase.py +++ b/tests/test_testcase.py @@ -419,18 +419,34 @@ class TestcaseParserUnittest(unittest.TestCase): testsets_list = testcase.load_testcases_by_path(path) self.assertIn("variable_binds", testsets_list[0]["config"]) self.assertIn("request", testsets_list[0]["config"]) - print(testsets_list[0]["testcases"][0]) self.assertIn("request", testsets_list[0]["testcases"][0]) self.assertIn("url", testsets_list[0]["testcases"][0]["request"]) self.assertIn("validators", testsets_list[0]["testcases"][0]) + def test_substitute_variables_with_mapping(self): + content = { + 'request': { + 'url': '/api/users/$uid', + 'method': "$method", + 'headers': {'token': '$token'} + } + } + mapping = { + "$uid": 1000, + "$method": "POST" + } + result = testcase.substitute_variables_with_mapping(content, mapping) + self.assertEqual("/api/users/1000", result["request"]["url"]) + self.assertEqual("$token", result["request"]["headers"]["token"]) + self.assertEqual("POST", result["request"]["method"]) + def test_load_api_definition(self): path = os.path.join( os.getcwd(), 'tests/data') api_dir_dict = testcase.load_api_definition(path) self.assertIn("get_token", api_dir_dict) self.assertEqual("/api/get-token", api_dir_dict["get_token"]["request"]["url"]) - self.assertIn("$user_name", api_dir_dict["get_token"]["function_meta"]["args"]) + self.assertIn("$user_agent", api_dir_dict["get_token"]["function_meta"]["args"]) self.assertIn("create_user", api_dir_dict) def test_get_api_definition(self):