From 0181e61ab8e8c060fb3589071ed82de7b5d100f7 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 19 Sep 2017 17:07:07 +0800 Subject: [PATCH] run_testset: add variables_mapping parameter --- ate/runner.py | 61 +++++++++++++++++++++++++++++--------------- tests/test_runner.py | 11 ++++++++ 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/ate/runner.py b/ate/runner.py index e5cb3d20..5128c43c 100644 --- a/ate/runner.py +++ b/ate/runner.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + from ate import exception, response, utils from ate.client import HttpSession from ate.context import Context @@ -121,29 +123,33 @@ class Runner(object): return True - def run_testset(self, testset): + def run_testset(self, testset, variables_mapping=None): """ run single testset, including one or several testcases. - @param (dict) testset - { - "name": "testset description", - "config": { + @param + (dict) testset + { "name": "testset description", - "requires": [], - "function_binds": {}, - "variable_binds": [], - "request": {} - }, - "testcases": [ - { - "name": "testcase description", - "variable_binds": [], # optional, override - "request": {}, - "extract_binds": {}, # optional - "validators": {} # optional + "config": { + "name": "testset description", + "requires": [], + "function_binds": {}, + "variable_binds": [], + "request": {} }, - testcase12 - ] - } + "testcases": [ + { + "name": "testcase description", + "variable_binds": [], # optional, override + "request": {}, + "extract_binds": {}, # optional + "validators": {} # optional + }, + testcase12 + ] + } + (dict) variables_mapping: + passed in variables mapping, it will override variable_binds in config block + @return (dict) test result of testcases { "success": True, @@ -152,6 +158,21 @@ class Runner(object): """ success = True config_dict = testset.get("config", {}) + + def merge_variable_binds(variable_binds, variables_mapping): + variables_dict = OrderedDict() + for variable_dict in variable_binds: + variables_dict.update(variable_dict) + + for var, value in variables_mapping.items(): + variables_dict.update({var: value}) + + return [{var: value} for var, value in variables_dict.items()] + + variable_binds = config_dict.get("variable_binds", []) + variables_mapping = variables_mapping or {} + config_dict["variable_binds"] = merge_variable_binds(variable_binds, variables_mapping) + self.init_config(config_dict, level="testset") testcases = testset.get("testcases", []) for testcase in testcases: diff --git a/tests/test_runner.py b/tests/test_runner.py index be8ceea2..185e43f2 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -119,3 +119,14 @@ class TestRunner(ApiServerUnittest): result = self.test_runner.run_testset(testsets[0]) self.assertTrue(result["success"]) self.assertIn("token", result["output"]) + + def test_run_testset_with_variables_mapping(self): + testcase_file_path = os.path.join( + os.getcwd(), 'tests/data/demo_testset_layer.yml') + testsets = load_testcases_by_path(testcase_file_path) + variables_mapping = { + "app_version": '2.9.7' + } + result = self.test_runner.run_testset(testsets[0], variables_mapping) + self.assertTrue(result["success"]) + self.assertIn("token", result["output"])