From 388a6da23aff337e8dfdef726a465a7914d2fc32 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 24 Oct 2017 15:59:09 +0800 Subject: [PATCH] rename keyword: variable_binds => variables --- README.md | 2 +- ate/context.py | 15 ++++---- ate/runner.py | 22 +++++------ ate/utils.py | 16 ++++---- docs/quickstart.md | 10 ++--- examples/quickstart-demo-rev-2.yml | 2 +- examples/quickstart-demo-rev-3.yml | 4 +- tests/data/demo_binds.yml | 8 ++-- tests/data/demo_testset_layer.yml | 10 ++--- ...demo_testset_template_import_functions.yml | 4 +- ...demo_testset_template_lambda_functions.yml | 4 +- tests/data/demo_testset_variables.yml | 6 +-- tests/test_context.py | 38 +++++++++---------- tests/test_testcase.py | 6 +-- 14 files changed, 74 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 9a8f7d8b..6f97d7c5 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ And here is testset example of typical scenario: get `token` at the beginning, a ```yaml - config: name: "create user testsets." - variable_binds: + variables: - user_agent: 'iOS/10.3' - device_sn: ${gen_random_string(15)} - os_platform: 'ios' diff --git a/ate/context.py b/ate/context.py index 60af1d52..3c809930 100644 --- a/ate/context.py +++ b/ate/context.py @@ -54,8 +54,9 @@ class Context(object): or config_dict.get('import_module_functions', []) self.import_module_items(module_items, level) - variable_binds = config_dict.get('variable_binds', OrderedDict()) - self.bind_variables(variable_binds, level) + variables = config_dict.get('variables') \ + or config_dict.get('variable_binds', OrderedDict()) + self.bind_variables(variables, level) def import_requires(self, modules): """ import required modules dynamically @@ -92,11 +93,11 @@ class Context(object): imported_variables_dict = utils.filter_module(imported_module, "variable") self.bind_variables(imported_variables_dict, level) - def bind_variables(self, variable_binds, level="testcase"): + def bind_variables(self, variables, level="testcase"): """ bind variables to testset context or current testcase context. variables in testset context can be used in all testcases of current test suite. - @param (list or OrderDict) variable_binds, variable can be value or custom function. + @param (list or OrderDict) variables, variable can be value or custom function. if value is function, it will be called and bind result to variable. e.g. OrderDict({ @@ -106,10 +107,10 @@ class Context(object): "md5": "${gen_md5($TOKEN, $json, $random)}" }) """ - if isinstance(variable_binds, list): - variable_binds = utils.convert_to_order_dict(variable_binds) + if isinstance(variables, list): + variables = utils.convert_to_order_dict(variables) - for variable_name, value in variable_binds.items(): + for variable_name, value in variables.items(): variable_evale_value = self.testcase_parser.parse_content_with_bindings(value) if level == "testset": diff --git a/ate/runner.py b/ate/runner.py index bd44e62f..b84e9206 100644 --- a/ate/runner.py +++ b/ate/runner.py @@ -23,7 +23,7 @@ class Runner(object): "requires": [], # optional "function_binds": {}, # optional "import_module_items": [], # optional - "variable_binds": [], # optional + "variables": [], # optional "request": { "base_url": "http://127.0.0.1:5000", "headers": { @@ -37,7 +37,7 @@ class Runner(object): "requires": [], # optional "function_binds": {}, # optional "import_module_items": [], # optional - "variable_binds": [], # optional + "variables": [], # optional "request": { "url": "/api/get-token", "method": "POST", @@ -71,9 +71,9 @@ class Runner(object): { "name": "testcase description", "times": 3, - "requires": [], # optional, override - "function_binds": {}, # optional, override - "variable_binds": [], # optional, override + "requires": [], # optional, override + "function_binds": {}, # optional, override + "variables": [], # optional, override "request": { "url": "http://127.0.0.1:5000/api/users/1000", "method": "POST", @@ -136,13 +136,13 @@ class Runner(object): "name": "testset description", "requires": [], "function_binds": {}, - "variable_binds": [], + "variables": [], "request": {} }, "testcases": [ { "name": "testcase description", - "variable_binds": [], # optional, override + "variables": [], # optional, override "request": {}, "extractors": {}, # optional "validators": {} # optional @@ -151,7 +151,7 @@ class Runner(object): ] } (dict) variables_mapping: - passed in variables mapping, it will override variable_binds in config block + passed in variables mapping, it will override variables in config block @return (dict) test result of testset { @@ -162,9 +162,9 @@ class Runner(object): success = True config_dict = testset.get("config", {}) - variable_binds = config_dict.get("variable_binds", []) + variables = config_dict.get("variables", []) variables_mapping = variables_mapping or {} - config_dict["variable_binds"] = utils.override_variables_binds(variable_binds, variables_mapping) + config_dict["variables"] = utils.override_variables_binds(variables, variables_mapping) self.init_config(config_dict, level="testset") testcases = testset.get("testcases", []) @@ -189,7 +189,7 @@ class Runner(object): - absolute/relative folder path - list/set container with file(s) and/or folder(s) (dict) mapping: - passed in variables mapping, it will override variable_binds in config block + passed in variables mapping, it will override variables in config block """ success = True mapping = mapping or {} diff --git a/ate/utils.py b/ate/utils.py index 33f8c694..e05eb45e 100644 --- a/ate/utils.py +++ b/ate/utils.py @@ -369,18 +369,18 @@ def update_ordered_dict(ordered_dict, override_mapping): return ordered_dict -def override_variables_binds(variable_binds, new_mapping): - """ convert variable_binds in testcase to ordered mapping, with new_mapping overrided +def override_variables_binds(variables, new_mapping): + """ convert variables in testcase to ordered mapping, with new_mapping overrided """ - if isinstance(variable_binds, list): - variable_binds_ordered_dict = convert_to_order_dict(variable_binds) - elif isinstance(variable_binds, OrderedDict): - variable_binds_ordered_dict = variable_binds + if isinstance(variables, list): + variables_ordered_dict = convert_to_order_dict(variables) + elif isinstance(variables, OrderedDict): + variables_ordered_dict = variables else: - raise exception.ParamsError("variable_binds error!") + raise exception.ParamsError("variables error!") return update_ordered_dict( - variable_binds_ordered_dict, + variables_ordered_dict, new_mapping ) diff --git a/docs/quickstart.md b/docs/quickstart.md index 500e7496..f1756da9 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -154,7 +154,7 @@ In actual scenarios, each user's `device_sn` is different, so we should paramete However, the test cases are only `YAML` documents, it is impossible to generate parameters dynamically in such text. Fortunately, we can combine `Python` scripts with `YAML/JSON` test cases in `ApiTestEngine`. -To achieve this goal, we can utilize `debugtalk.py` plugin and `variable_binds` mechanisms. +To achieve this goal, we can utilize `debugtalk.py` plugin and `variables` mechanisms. To be specific, we can create a Python file (`examples/debugtalk.py`) and implement the related algorithm in it. The `debugtalk.py` file can not only be located beside `YAML/JSON` testset file, but also can be in any upward recursive folder. Since we want `debugtalk.py` to be importable, we should put a `__init__.py` in its folder to make it as a Python module. @@ -187,7 +187,7 @@ And then, we can revise our demo test case and reference the functions. Suppose ```yaml - test: name: get token - variable_binds: + variables: - user_agent: 'iOS/10.3' - device_sn: ${gen_random_string(15)} - os_platform: 'ios' @@ -226,7 +226,7 @@ And then, we can revise our demo test case and reference the functions. Suppose In this revised test case, `variable reference` and `function invoke` mechanisms are both used. -To make fields like `device_sn` can be used more than once, we bind values to variables in `variable_binds` block. When we bind variables, we can not only bind exact value to a variable name, but also can call a function and bind the evaluated value to it. +To make fields like `device_sn` can be used more than once, we bind values to variables in `variables` block. When we bind variables, we can not only bind exact value to a variable name, but also can call a function and bind the evaluated value to it. When we want to reference a variable in the test case, we can do this with a escape character `$`. For example, `$user_agent` will not be taken as a normal string, and `ApiTestEngine` will consider it as a variable named `user_agent`, search and return its binding value. @@ -244,7 +244,7 @@ To handle this case, overall `config` block is supported in `ApiTestEngine`. If # examples/quickstart-demo-rev-3.yml - config: name: "smoketest for CRUD users." - variable_binds: + variables: - device_sn: ${gen_random_string(15)} request: base_url: http://127.0.0.1:5000 @@ -253,7 +253,7 @@ To handle this case, overall `config` block is supported in `ApiTestEngine`. If - test: name: get token - variable_binds: + variables: - user_agent: 'iOS/10.3' - os_platform: 'ios' - app_version: '2.8.6' diff --git a/examples/quickstart-demo-rev-2.yml b/examples/quickstart-demo-rev-2.yml index 540a4ad3..7b2282f8 100644 --- a/examples/quickstart-demo-rev-2.yml +++ b/examples/quickstart-demo-rev-2.yml @@ -1,6 +1,6 @@ - test: name: get token - variable_binds: + variables: - user_agent: 'iOS/10.3' - device_sn: ${gen_random_string(15)} - os_platform: 'ios' diff --git a/examples/quickstart-demo-rev-3.yml b/examples/quickstart-demo-rev-3.yml index 44c3bbbf..cb937011 100644 --- a/examples/quickstart-demo-rev-3.yml +++ b/examples/quickstart-demo-rev-3.yml @@ -1,6 +1,6 @@ - config: name: "smoketest for CRUD users." - variable_binds: + variables: - device_sn: ${gen_random_string(15)} request: base_url: http://127.0.0.1:5000 @@ -9,7 +9,7 @@ - test: name: get token - variable_binds: + variables: - user_agent: 'iOS/10.3' - os_platform: 'ios' - app_version: '2.8.6' diff --git a/tests/data/demo_binds.yml b/tests/data/demo_binds.yml index 18135cc7..1ceb4db9 100644 --- a/tests/data/demo_binds.yml +++ b/tests/data/demo_binds.yml @@ -1,5 +1,5 @@ bind_variables: - variable_binds: + variables: - TOKEN: "debugtalk" - token: $TOKEN @@ -7,7 +7,7 @@ bind_lambda_functions: function_binds: add_one: "lambda x: x + 1" add_two_nums: "lambda x, y: x + y" - variable_binds: + variables: - add1: ${add_one(2)} - sum2nums: ${add_two_nums(2, 3)} @@ -19,7 +19,7 @@ bind_lambda_functions_with_import: function_binds: gen_random_string: "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" - variable_binds: + variables: - TOKEN: debugtalk - random: ${gen_random_string(5)} - data: "{'name': 'user', 'password': '123456'}" @@ -29,7 +29,7 @@ bind_module_functions: function_binds: import_module_items: - tests.data.debugtalk - variable_binds: + variables: - TOKEN: debugtalk - random: ${gen_random_string(5)} - data: "{'name': 'user', 'password': '123456'}" diff --git a/tests/data/demo_testset_layer.yml b/tests/data/demo_testset_layer.yml index ba22272f..44102750 100644 --- a/tests/data/demo_testset_layer.yml +++ b/tests/data/demo_testset_layer.yml @@ -1,6 +1,6 @@ - config: name: "user management testset." - variable_binds: + variables: - user_agent: 'iOS/10.3' - device_sn: ${gen_random_string(15)} - os_platform: 'ios' @@ -35,7 +35,7 @@ - test: name: create user which does not exist - variable_binds: + variables: - user_name: "user1" - user_password: "123456" api: create_user(1000, $user_name, $user_password, $token) @@ -53,7 +53,7 @@ - test: name: create user which exists - variable_binds: + variables: - user_name: "user1" - user_password: "123456" api: create_user(1000, $user_name, $user_password, $token) @@ -63,7 +63,7 @@ - test: name: update user which exists - variable_binds: + variables: - user_name: "user1" - user_password: "654321" api: update_user(1000, $user_name, $user_password, $token) @@ -102,7 +102,7 @@ - test: name: create user which has been deleted - variable_binds: + variables: - user_name: "user1" - user_password: "123456" api: create_user(1000, $user_name, $user_password, $token) diff --git a/tests/data/demo_testset_template_import_functions.yml b/tests/data/demo_testset_template_import_functions.yml index 28ffdbb7..fc710cf8 100644 --- a/tests/data/demo_testset_template_import_functions.yml +++ b/tests/data/demo_testset_template_import_functions.yml @@ -2,7 +2,7 @@ name: "create user testsets." import_module_items: - tests.data.debugtalk - variable_binds: + variables: - user_agent: 'iOS/10.3' - device_sn: ${gen_random_string(15)} - os_platform: 'ios' @@ -33,7 +33,7 @@ - test: name: create user which does not exist - variable_binds: + variables: - user_name: "user1" - user_password: "123456" request: diff --git a/tests/data/demo_testset_template_lambda_functions.yml b/tests/data/demo_testset_template_lambda_functions.yml index 26d5eecf..7e450aa6 100644 --- a/tests/data/demo_testset_template_lambda_functions.yml +++ b/tests/data/demo_testset_template_lambda_functions.yml @@ -12,7 +12,7 @@ 'DebugTalk'.encode('ascii'), ''.join(args).encode('ascii'), hashlib.sha1).hexdigest()" - variable_binds: + variables: - user_agent: 'iOS/10.3' - device_sn: ${gen_random_string_lambda(15)} - os_platform: 'ios' @@ -43,7 +43,7 @@ - test: name: create user which does not exist - variable_binds: + variables: - user_name: "user1" - user_password: "123456" request: diff --git a/tests/data/demo_testset_variables.yml b/tests/data/demo_testset_variables.yml index 98dd3d16..56e31fd6 100644 --- a/tests/data/demo_testset_variables.yml +++ b/tests/data/demo_testset_variables.yml @@ -1,6 +1,6 @@ - config: name: "create user testsets." - variable_binds: + variables: - device_sn: 'HZfFBh6tU59EdXJ' request: base_url: $BASE_URL @@ -10,7 +10,7 @@ - test: name: get token - variable_binds: + variables: - user_agent: 'iOS/10.3' - os_platform: 'ios' - app_version: '2.8.6' @@ -34,7 +34,7 @@ - test: name: create user which does not exist - variable_binds: + variables: - user_name: "user1" - user_password: "123456" request: diff --git a/tests/test_context.py b/tests/test_context.py index a1fc774f..ce07f7af 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -18,11 +18,11 @@ class VariableBindsUnittest(unittest.TestCase): self.assertIn("get_timestamp", self.context.testset_functions_config) self.assertIn("gen_random_string", self.context.testset_functions_config) - variable_binds = [ + variables = [ {"random": "${gen_random_string(5)}"}, {"timestamp10": "${get_timestamp(10)}"} ] - self.context.bind_variables(variable_binds) + self.context.bind_variables(variables) context_variables = self.context.get_testcase_variables_mapping() self.assertEqual(len(context_variables["random"]), 5) @@ -31,7 +31,7 @@ class VariableBindsUnittest(unittest.TestCase): def test_context_bind_testset_variables(self): # testcase in JSON format testcase1 = { - "variable_binds": [ + "variables": [ {"GLOBAL_TOKEN": "debugtalk"}, {"token": "$GLOBAL_TOKEN"} ] @@ -40,8 +40,8 @@ class VariableBindsUnittest(unittest.TestCase): testcase2 = self.testcases["bind_variables"] for testcase in [testcase1, testcase2]: - variable_binds = testcase['variable_binds'] - self.context.bind_variables(variable_binds, level="testset") + variables = testcase['variables'] + self.context.bind_variables(variables, level="testset") testset_variables = self.context.testset_shared_variables_mapping testcase_variables = self.context.get_testcase_variables_mapping() @@ -54,7 +54,7 @@ class VariableBindsUnittest(unittest.TestCase): def test_context_bind_testcase_variables(self): testcase1 = { - "variable_binds": [ + "variables": [ {"GLOBAL_TOKEN": "debugtalk"}, {"token": "$GLOBAL_TOKEN"} ] @@ -62,8 +62,8 @@ class VariableBindsUnittest(unittest.TestCase): testcase2 = self.testcases["bind_variables"] for testcase in [testcase1, testcase2]: - variable_binds = testcase['variable_binds'] - self.context.bind_variables(variable_binds) + variables = testcase['variables'] + self.context.bind_variables(variables) testset_variables = self.context.testset_shared_variables_mapping testcase_variables = self.context.get_testcase_variables_mapping() @@ -80,7 +80,7 @@ class VariableBindsUnittest(unittest.TestCase): "add_one": lambda x: x + 1, "add_two_nums": lambda x, y: x + y }, - "variable_binds": [ + "variables": [ {"add1": "${add_one(2)}"}, {"sum2nums": "${add_two_nums(2,3)}"} ] @@ -91,8 +91,8 @@ class VariableBindsUnittest(unittest.TestCase): function_binds = testcase.get('function_binds', {}) self.context.bind_functions(function_binds) - variable_binds = testcase['variable_binds'] - self.context.bind_variables(variable_binds) + variables = testcase['variables'] + self.context.bind_variables(variables) context_variables = self.context.get_testcase_variables_mapping() self.assertIn("add1", context_variables) @@ -107,7 +107,7 @@ class VariableBindsUnittest(unittest.TestCase): "gen_random_string": "lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(str_len))", "gen_md5": "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" }, - "variable_binds": [ + "variables": [ {"TOKEN": "debugtalk"}, {"random": "${gen_random_string(5)}"}, {"data": '{"name": "user", "password": "123456"}'}, @@ -123,8 +123,8 @@ class VariableBindsUnittest(unittest.TestCase): function_binds = testcase.get('function_binds', {}) self.context.bind_functions(function_binds) - variable_binds = testcase['variable_binds'] - self.context.bind_variables(variable_binds) + variables = testcase['variables'] + self.context.bind_variables(variables) context_variables = self.context.get_testcase_variables_mapping() self.assertIn("TOKEN", context_variables) @@ -144,7 +144,7 @@ class VariableBindsUnittest(unittest.TestCase): def test_import_module_items(self): testcase1 = { "import_module_items": ["tests.data.debugtalk"], - "variable_binds": [ + "variables": [ {"TOKEN": "debugtalk"}, {"random": "${gen_random_string(5)}"}, {"data": '{"name": "user", "password": "123456"}'}, @@ -157,8 +157,8 @@ class VariableBindsUnittest(unittest.TestCase): module_items = testcase.get('import_module_items', []) self.context.import_module_items(module_items) - variable_binds = testcase['variable_binds'] - self.context.bind_variables(variable_binds) + variables = testcase['variables'] + self.context.bind_variables(variables) context_variables = self.context.get_testcase_variables_mapping() self.assertIn("TOKEN", context_variables) @@ -182,7 +182,7 @@ class VariableBindsUnittest(unittest.TestCase): test_runner = runner.Runner() testcase = { "import_module_items": ["tests.data.debugtalk"], - "variable_binds": [ + "variables": [ {"TOKEN": "debugtalk"}, {"random": "${gen_random_string(5)}"}, {"data": '{"name": "user", "password": "123456"}'}, @@ -206,7 +206,7 @@ class VariableBindsUnittest(unittest.TestCase): self.assertIn("random", parsed_request["headers"]) self.assertEqual(len(parsed_request["headers"]["random"]), 5) self.assertIn("data", parsed_request) - self.assertEqual(parsed_request["data"], testcase["variable_binds"][2]["data"]) + self.assertEqual(parsed_request["data"], testcase["variables"][2]["data"]) self.assertEqual(parsed_request["headers"]["SECRET_KEY"], "DebugTalk") def test_exec_content_functions(self): diff --git a/tests/test_testcase.py b/tests/test_testcase.py index a94d2d93..633d9f8a 100644 --- a/tests/test_testcase.py +++ b/tests/test_testcase.py @@ -55,7 +55,7 @@ class TestcaseParserUnittest(unittest.TestCase): ) def test_eval_content_variables(self): - variable_binds = { + variables = { "var_1": "abc", "var_2": "def", "var_3": 123, @@ -63,7 +63,7 @@ class TestcaseParserUnittest(unittest.TestCase): "var_5": True, "var_6": None } - testcase_parser = testcase.TestcaseParser(variables_binds=variable_binds) + testcase_parser = testcase.TestcaseParser(variables_binds=variables) self.assertEqual( testcase_parser.eval_content_variables("$var_1"), "abc" @@ -417,7 +417,7 @@ class TestcaseParserUnittest(unittest.TestCase): path = os.path.join( os.getcwd(), 'tests/data/demo_testset_layer.yml') testsets_list = testcase.load_testcases_by_path(path) - self.assertIn("variable_binds", testsets_list[0]["config"]) + self.assertIn("variables", testsets_list[0]["config"]) self.assertIn("request", testsets_list[0]["config"]) self.assertIn("request", testsets_list[0]["testcases"][0]) self.assertIn("url", testsets_list[0]["testcases"][0]["request"])