run_testset: add variables_mapping parameter

This commit is contained in:
debugtalk
2017-09-19 17:07:07 +08:00
parent 1414d27517
commit 0181e61ab8
2 changed files with 52 additions and 20 deletions
+41 -20
View File
@@ -1,3 +1,5 @@
from collections import OrderedDict
from ate import exception, response, utils from ate import exception, response, utils
from ate.client import HttpSession from ate.client import HttpSession
from ate.context import Context from ate.context import Context
@@ -121,29 +123,33 @@ class Runner(object):
return True return True
def run_testset(self, testset): def run_testset(self, testset, variables_mapping=None):
""" run single testset, including one or several testcases. """ run single testset, including one or several testcases.
@param (dict) testset @param
{ (dict) testset
"name": "testset description", {
"config": {
"name": "testset description", "name": "testset description",
"requires": [], "config": {
"function_binds": {}, "name": "testset description",
"variable_binds": [], "requires": [],
"request": {} "function_binds": {},
}, "variable_binds": [],
"testcases": [ "request": {}
{
"name": "testcase description",
"variable_binds": [], # optional, override
"request": {},
"extract_binds": {}, # optional
"validators": {} # optional
}, },
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 @return (dict) test result of testcases
{ {
"success": True, "success": True,
@@ -152,6 +158,21 @@ class Runner(object):
""" """
success = True success = True
config_dict = testset.get("config", {}) 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") self.init_config(config_dict, level="testset")
testcases = testset.get("testcases", []) testcases = testset.get("testcases", [])
for testcase in testcases: for testcase in testcases:
+11
View File
@@ -119,3 +119,14 @@ class TestRunner(ApiServerUnittest):
result = self.test_runner.run_testset(testsets[0]) result = self.test_runner.run_testset(testsets[0])
self.assertTrue(result["success"]) self.assertTrue(result["success"])
self.assertIn("token", result["output"]) 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"])