mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
refactor ate/testcase.py: organise code with TestcaseParser, reduce passing parameters
This commit is contained in:
@@ -39,7 +39,7 @@ To ensure the installation or upgrade is successful, you can execute command `at
|
|||||||
```text
|
```text
|
||||||
$ ate -V
|
$ ate -V
|
||||||
jenkins-mail-py version: 0.2.5
|
jenkins-mail-py version: 0.2.5
|
||||||
ApiTestEngine version: 0.4.0
|
ApiTestEngine version: 0.5.0
|
||||||
```
|
```
|
||||||
|
|
||||||
Execute the command `ate -h` to view command help.
|
Execute the command `ate -h` to view command help.
|
||||||
|
|||||||
+11
-10
@@ -6,7 +6,8 @@ import sys
|
|||||||
import types
|
import types
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from ate import testcase, utils
|
from ate.testcase import TestcaseParser
|
||||||
|
from ate import utils
|
||||||
|
|
||||||
|
|
||||||
def is_function(tup):
|
def is_function(tup):
|
||||||
@@ -22,6 +23,7 @@ class Context(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.testset_shared_variables_mapping = OrderedDict()
|
self.testset_shared_variables_mapping = OrderedDict()
|
||||||
self.testcase_variables_mapping = OrderedDict()
|
self.testcase_variables_mapping = OrderedDict()
|
||||||
|
self.testcase_parser = TestcaseParser()
|
||||||
self.init_context()
|
self.init_context()
|
||||||
|
|
||||||
def init_context(self, level='testset'):
|
def init_context(self, level='testset'):
|
||||||
@@ -40,6 +42,9 @@ class Context(object):
|
|||||||
self.testcase_request_config = {}
|
self.testcase_request_config = {}
|
||||||
self.testcase_variables_mapping = copy.deepcopy(self.testset_shared_variables_mapping)
|
self.testcase_variables_mapping = copy.deepcopy(self.testset_shared_variables_mapping)
|
||||||
|
|
||||||
|
self.testcase_parser.bind_functions(self.testcase_functions_config)
|
||||||
|
self.testcase_parser.bind_variables(self.testcase_variables_mapping)
|
||||||
|
|
||||||
def import_requires(self, modules):
|
def import_requires(self, modules):
|
||||||
""" import required modules dynamicly
|
""" import required modules dynamicly
|
||||||
"""
|
"""
|
||||||
@@ -88,16 +93,13 @@ class Context(object):
|
|||||||
"""
|
"""
|
||||||
for variable_bind in variable_binds:
|
for variable_bind in variable_binds:
|
||||||
for variable_name, value in variable_bind.items():
|
for variable_name, value in variable_bind.items():
|
||||||
variable_evale_value = testcase.parse_content_with_bindings(
|
variable_evale_value = self.testcase_parser.parse_content_with_bindings(value)
|
||||||
value,
|
|
||||||
self.testcase_variables_mapping,
|
|
||||||
self.testcase_functions_config
|
|
||||||
)
|
|
||||||
|
|
||||||
if level == "testset":
|
if level == "testset":
|
||||||
self.testset_shared_variables_mapping[variable_name] = variable_evale_value
|
self.testset_shared_variables_mapping[variable_name] = variable_evale_value
|
||||||
|
|
||||||
self.testcase_variables_mapping[variable_name] = variable_evale_value
|
self.testcase_variables_mapping[variable_name] = variable_evale_value
|
||||||
|
self.testcase_parser.bind_variables(self.testcase_variables_mapping)
|
||||||
|
|
||||||
def __update_context_functions_config(self, level, config_mapping):
|
def __update_context_functions_config(self, level, config_mapping):
|
||||||
"""
|
"""
|
||||||
@@ -109,6 +111,7 @@ class Context(object):
|
|||||||
self.testset_functions_config.update(config_mapping)
|
self.testset_functions_config.update(config_mapping)
|
||||||
|
|
||||||
self.testcase_functions_config.update(config_mapping)
|
self.testcase_functions_config.update(config_mapping)
|
||||||
|
self.testcase_parser.bind_functions(self.testcase_functions_config)
|
||||||
|
|
||||||
def register_request(self, request_dict, level="testcase"):
|
def register_request(self, request_dict, level="testcase"):
|
||||||
self.__update_context_request_config(level, request_dict)
|
self.__update_context_request_config(level, request_dict)
|
||||||
@@ -130,10 +133,8 @@ class Context(object):
|
|||||||
def get_parsed_request(self):
|
def get_parsed_request(self):
|
||||||
""" get parsed request, with each variable replaced by bind value.
|
""" get parsed request, with each variable replaced by bind value.
|
||||||
"""
|
"""
|
||||||
parsed_request = testcase.parse_content_with_bindings(
|
parsed_request = self.testcase_parser.parse_content_with_bindings(
|
||||||
self.testcase_request_config,
|
self.testcase_request_config
|
||||||
self.testcase_variables_mapping,
|
|
||||||
self.testcase_functions_config
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return parsed_request
|
return parsed_request
|
||||||
|
|||||||
+100
-89
@@ -24,44 +24,8 @@ def extract_variables(content):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def eval_content_variables(content, variable_mapping):
|
|
||||||
""" replace all variables of string content with mapping value.
|
|
||||||
@param (str) content
|
|
||||||
@return (str) parsed content
|
|
||||||
|
|
||||||
e.g.
|
|
||||||
variable_mapping = {
|
|
||||||
"var_1": "abc",
|
|
||||||
"var_2": "def"
|
|
||||||
}
|
|
||||||
$var_1 => "abc"
|
|
||||||
$var_1#XYZ => "abc#XYZ"
|
|
||||||
/$var_1/$var_2/var3 => "/abc/def/var3"
|
|
||||||
${func($var_1, $var_2, xyz)} => "${func(abc, def, xyz)}"
|
|
||||||
"""
|
|
||||||
variables_list = extract_variables(content)
|
|
||||||
for variable_name in variables_list:
|
|
||||||
if variable_name not in variable_mapping:
|
|
||||||
raise ParamsError(
|
|
||||||
"%s is not defined in bind variables!" % variable_name)
|
|
||||||
|
|
||||||
variable_value = variable_mapping.get(variable_name)
|
|
||||||
if "${}".format(variable_name) == content:
|
|
||||||
# content is a variable
|
|
||||||
content = variable_value
|
|
||||||
else:
|
|
||||||
# content contains one or many variables
|
|
||||||
content = content.replace(
|
|
||||||
"${}".format(variable_name),
|
|
||||||
str(variable_value), 1
|
|
||||||
)
|
|
||||||
|
|
||||||
return content
|
|
||||||
|
|
||||||
def extract_functions(content):
|
def extract_functions(content):
|
||||||
""" extract all functions from string content, which are in format ${fun()}
|
""" extract all functions from string content, which are in format ${fun()}
|
||||||
Notice: extract_functions should be called after eval_content_variables, thus
|
|
||||||
there will not be any variables in given content
|
|
||||||
@param (str) content
|
@param (str) content
|
||||||
@return (list) functions list
|
@return (list) functions list
|
||||||
|
|
||||||
@@ -76,35 +40,6 @@ def extract_functions(content):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def eval_content_functions(content, variables_binds, functions_binds):
|
|
||||||
functions_list = extract_functions(content)
|
|
||||||
for func_content in functions_list:
|
|
||||||
function_meta = parse_function(func_content)
|
|
||||||
func_name = function_meta['func_name']
|
|
||||||
|
|
||||||
func = functions_binds.get(func_name)
|
|
||||||
if func is None:
|
|
||||||
raise ParamsError(
|
|
||||||
"%s is not defined in bind functions!" % func_name)
|
|
||||||
|
|
||||||
args = function_meta.get('args', [])
|
|
||||||
kwargs = function_meta.get('kwargs', {})
|
|
||||||
args = parse_content_with_bindings(args, variables_binds, functions_binds)
|
|
||||||
kwargs = parse_content_with_bindings(kwargs, variables_binds, functions_binds)
|
|
||||||
eval_value = func(*args, **kwargs)
|
|
||||||
|
|
||||||
if func_content == content:
|
|
||||||
# content is a variable
|
|
||||||
content = eval_value
|
|
||||||
else:
|
|
||||||
# content contains one or many variables
|
|
||||||
content = content.replace(
|
|
||||||
func_content,
|
|
||||||
str(eval_value), 1
|
|
||||||
)
|
|
||||||
|
|
||||||
return content
|
|
||||||
|
|
||||||
def parse_string_value(str_value):
|
def parse_string_value(str_value):
|
||||||
""" parse string to number if possible
|
""" parse string to number if possible
|
||||||
e.g. "123" => 123
|
e.g. "123" => 123
|
||||||
@@ -152,14 +87,103 @@ def parse_function(content):
|
|||||||
|
|
||||||
return function_meta
|
return function_meta
|
||||||
|
|
||||||
def parse_content_with_bindings(content, variables_binds, functions_binds):
|
def eval_content_variables(content, variable_mapping):
|
||||||
""" evaluate content recursively, each variable in content will be
|
""" replace all variables of string content with mapping value.
|
||||||
evaluated with bind variables and functions.
|
@param (str) content
|
||||||
|
@return (str) parsed content
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
variable_mapping = {
|
||||||
|
"var_1": "abc",
|
||||||
|
"var_2": "def"
|
||||||
|
}
|
||||||
|
$var_1 => "abc"
|
||||||
|
$var_1#XYZ => "abc#XYZ"
|
||||||
|
/$var_1/$var_2/var3 => "/abc/def/var3"
|
||||||
|
${func($var_1, $var_2, xyz)} => "${func(abc, def, xyz)}"
|
||||||
|
"""
|
||||||
|
variables_list = extract_variables(content)
|
||||||
|
for variable_name in variables_list:
|
||||||
|
if variable_name not in variable_mapping:
|
||||||
|
raise ParamsError(
|
||||||
|
"%s is not defined in bind variables!" % variable_name)
|
||||||
|
|
||||||
|
variable_value = variable_mapping.get(variable_name)
|
||||||
|
if "${}".format(variable_name) == content:
|
||||||
|
# content is a variable
|
||||||
|
content = variable_value
|
||||||
|
else:
|
||||||
|
# content contains one or many variables
|
||||||
|
content = content.replace(
|
||||||
|
"${}".format(variable_name),
|
||||||
|
str(variable_value), 1
|
||||||
|
)
|
||||||
|
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
class TestcaseParser(object):
|
||||||
|
|
||||||
|
def __init__(self, variables_binds={}, functions_binds={}):
|
||||||
|
self.bind_variables(variables_binds)
|
||||||
|
self.bind_functions(functions_binds)
|
||||||
|
|
||||||
|
def bind_variables(self, variables_binds):
|
||||||
|
""" bind variables to current testcase parser
|
||||||
|
@param (dict) variables_binds, variables binds mapping
|
||||||
|
{
|
||||||
|
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
|
||||||
|
"random": "A2dEx",
|
||||||
|
"data": {"name": "user", "password": "123456"},
|
||||||
|
"uuid": 1000
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
self.variables_binds = variables_binds
|
||||||
|
|
||||||
|
def bind_functions(self, functions_binds):
|
||||||
|
""" bind functions to current testcase parser
|
||||||
|
@param (dict) functions_binds, functions binds mapping
|
||||||
|
{
|
||||||
|
"add_two_nums": lambda a, b=1: a + b
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
self.functions_binds = functions_binds
|
||||||
|
|
||||||
|
def eval_content_functions(self, content):
|
||||||
|
functions_list = extract_functions(content)
|
||||||
|
for func_content in functions_list:
|
||||||
|
function_meta = parse_function(func_content)
|
||||||
|
func_name = function_meta['func_name']
|
||||||
|
|
||||||
|
func = self.functions_binds.get(func_name)
|
||||||
|
if func is None:
|
||||||
|
raise ParamsError(
|
||||||
|
"%s is not defined in bind functions!" % func_name)
|
||||||
|
|
||||||
|
args = function_meta.get('args', [])
|
||||||
|
kwargs = function_meta.get('kwargs', {})
|
||||||
|
args = self.parse_content_with_bindings(args)
|
||||||
|
kwargs = self.parse_content_with_bindings(kwargs)
|
||||||
|
eval_value = func(*args, **kwargs)
|
||||||
|
|
||||||
|
if func_content == content:
|
||||||
|
# content is a variable
|
||||||
|
content = eval_value
|
||||||
|
else:
|
||||||
|
# content contains one or many variables
|
||||||
|
content = content.replace(
|
||||||
|
func_content,
|
||||||
|
str(eval_value), 1
|
||||||
|
)
|
||||||
|
|
||||||
|
return content
|
||||||
|
|
||||||
|
def parse_content_with_bindings(self, content):
|
||||||
|
""" parse content recursively, each variable and function in content will be evaluated.
|
||||||
|
|
||||||
variables marker: $variable.
|
|
||||||
@param (dict) content in any data structure
|
@param (dict) content in any data structure
|
||||||
{
|
{
|
||||||
"url": "http://127.0.0.1:5000/api/users/$uid",
|
"url": "http://127.0.0.1:5000/api/users/$uid/${add_two_nums(1, 1)}",
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -169,20 +193,9 @@ def parse_content_with_bindings(content, variables_binds, functions_binds):
|
|||||||
},
|
},
|
||||||
"body": "$data"
|
"body": "$data"
|
||||||
}
|
}
|
||||||
@param (dict) variables_binds, variables binds mapping
|
|
||||||
{
|
|
||||||
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
|
|
||||||
"random": "A2dEx",
|
|
||||||
"data": {"name": "user", "password": "123456"},
|
|
||||||
"uuid": 1000
|
|
||||||
}
|
|
||||||
@param (dict) functions_binds, functions binds mapping
|
|
||||||
{
|
|
||||||
"add_two_nums": lambda a, b=1: a + b
|
|
||||||
}
|
|
||||||
@return (dict) parsed content with evaluated bind values
|
@return (dict) parsed content with evaluated bind values
|
||||||
{
|
{
|
||||||
"url": "http://127.0.0.1:5000/api/users/1000",
|
"url": "http://127.0.0.1:5000/api/users/1000/2",
|
||||||
"method": "POST",
|
"method": "POST",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -196,17 +209,15 @@ def parse_content_with_bindings(content, variables_binds, functions_binds):
|
|||||||
|
|
||||||
if isinstance(content, (list, tuple)):
|
if isinstance(content, (list, tuple)):
|
||||||
return [
|
return [
|
||||||
parse_content_with_bindings(item, variables_binds, functions_binds)
|
self.parse_content_with_bindings(item)
|
||||||
for item in content
|
for item in content
|
||||||
]
|
]
|
||||||
|
|
||||||
if isinstance(content, dict):
|
if isinstance(content, dict):
|
||||||
evaluated_data = {}
|
evaluated_data = {}
|
||||||
for key, value in content.items():
|
for key, value in content.items():
|
||||||
eval_key = parse_content_with_bindings(
|
eval_key = self.parse_content_with_bindings(key)
|
||||||
key, variables_binds, functions_binds)
|
eval_value = self.parse_content_with_bindings(value)
|
||||||
eval_value = parse_content_with_bindings(
|
|
||||||
value, variables_binds, functions_binds)
|
|
||||||
evaluated_data[eval_key] = eval_value
|
evaluated_data[eval_key] = eval_value
|
||||||
|
|
||||||
return evaluated_data
|
return evaluated_data
|
||||||
@@ -219,9 +230,9 @@ def parse_content_with_bindings(content, variables_binds, functions_binds):
|
|||||||
|
|
||||||
# replace functions with evaluated value
|
# replace functions with evaluated value
|
||||||
# Notice: eval_content_functions must be called before eval_content_variables
|
# Notice: eval_content_functions must be called before eval_content_variables
|
||||||
content = eval_content_functions(content, variables_binds, functions_binds)
|
content = self.eval_content_functions(content)
|
||||||
|
|
||||||
# replace variables with binding value
|
# replace variables with binding value
|
||||||
content = eval_content_variables(content, variables_binds)
|
content = eval_content_variables(content, self.variables_binds)
|
||||||
|
|
||||||
return content
|
return content
|
||||||
|
|||||||
+19
-14
@@ -149,24 +149,25 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
"str_1": "str_value1",
|
"str_1": "str_value1",
|
||||||
"str_2": "str_value2"
|
"str_2": "str_value2"
|
||||||
}
|
}
|
||||||
|
testcase_parser = testcase.TestcaseParser(variables_binds=variables_binds)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings("$str_1", variables_binds, {}),
|
testcase_parser.parse_content_with_bindings("$str_1"),
|
||||||
"str_value1"
|
"str_value1"
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings("123$str_1/456", variables_binds, {}),
|
testcase_parser.parse_content_with_bindings("123$str_1/456"),
|
||||||
"123str_value1/456"
|
"123str_value1/456"
|
||||||
)
|
)
|
||||||
|
|
||||||
with self.assertRaises(ParamsError):
|
with self.assertRaises(ParamsError):
|
||||||
testcase.parse_content_with_bindings("$str_3", variables_binds, {})
|
testcase_parser.parse_content_with_bindings("$str_3")
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings(["$str_1", "str3"], variables_binds, {}),
|
testcase_parser.parse_content_with_bindings(["$str_1", "str3"]),
|
||||||
["str_value1", "str3"]
|
["str_value1", "str3"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings({"key": "$str_1"}, variables_binds, {}),
|
testcase_parser.parse_content_with_bindings({"key": "$str_1"}),
|
||||||
{"key": "str_value1"}
|
{"key": "str_value1"}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -175,9 +176,10 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
"userid": 100,
|
"userid": 100,
|
||||||
"data": 1498
|
"data": 1498
|
||||||
}
|
}
|
||||||
|
testcase_parser = testcase.TestcaseParser(variables_binds=variables_binds)
|
||||||
content = "/users/$userid/training/$data?userId=$userid&data=$data"
|
content = "/users/$userid/training/$data?userId=$userid&data=$data"
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings(content, variables_binds, {}),
|
testcase_parser.parse_content_with_bindings(content),
|
||||||
"/users/100/training/1498?userId=100&data=1498"
|
"/users/100/training/1498?userId=100&data=1498"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -187,9 +189,10 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
"userid": 1000,
|
"userid": 1000,
|
||||||
"data": 1498
|
"data": 1498
|
||||||
}
|
}
|
||||||
|
testcase_parser = testcase.TestcaseParser(variables_binds=variables_binds)
|
||||||
content = "/users/$user/$userid/$data?userId=$userid&data=$data"
|
content = "/users/$user/$userid/$data?userId=$userid&data=$data"
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings(content, variables_binds, {}),
|
testcase_parser.parse_content_with_bindings(content),
|
||||||
"/users/100/1000/1498?userId=1000&data=1498"
|
"/users/100/1000/1498?userId=1000&data=1498"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -199,18 +202,19 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
"gen_random_string": lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \
|
"gen_random_string": lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \
|
||||||
for _ in range(str_len))
|
for _ in range(str_len))
|
||||||
}
|
}
|
||||||
|
testcase_parser = testcase.TestcaseParser(functions_binds=functions_binds)
|
||||||
|
|
||||||
result = testcase.parse_content_with_bindings("${gen_random_string(5)}", {}, functions_binds)
|
result = testcase_parser.parse_content_with_bindings("${gen_random_string(5)}")
|
||||||
self.assertEqual(len(result), 5)
|
self.assertEqual(len(result), 5)
|
||||||
|
|
||||||
add_two_nums = lambda a, b=1: a + b
|
add_two_nums = lambda a, b=1: a + b
|
||||||
functions_binds["add_two_nums"] = add_two_nums
|
functions_binds["add_two_nums"] = add_two_nums
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings("${add_two_nums(1)}", {}, functions_binds),
|
testcase_parser.parse_content_with_bindings("${add_two_nums(1)}"),
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_content_with_bindings("${add_two_nums(1, 2)}", {}, functions_binds),
|
testcase_parser.parse_content_with_bindings("${add_two_nums(1, 2)}"),
|
||||||
3
|
3
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -252,12 +256,13 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
functions_binds = {
|
functions_binds = {
|
||||||
"add_two_nums": lambda a, b=1: a + b
|
"add_two_nums": lambda a, b=1: a + b
|
||||||
}
|
}
|
||||||
|
testcase_parser = testcase.TestcaseParser(functions_binds=functions_binds)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.eval_content_functions("${add_two_nums(1, 2)}", {}, functions_binds),
|
testcase_parser.eval_content_functions("${add_two_nums(1, 2)}"),
|
||||||
3
|
3
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.eval_content_functions("/api/${add_two_nums(1, 2)}", {}, functions_binds),
|
testcase_parser.eval_content_functions("/api/${add_two_nums(1, 2)}"),
|
||||||
"/api/3"
|
"/api/3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -283,8 +288,8 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
},
|
},
|
||||||
"body": "$data"
|
"body": "$data"
|
||||||
}
|
}
|
||||||
parsed_testcase = testcase.parse_content_with_bindings(
|
parsed_testcase = testcase.TestcaseParser(variables_binds, functions_binds)\
|
||||||
testcase_template, variables_binds, functions_binds)
|
.parse_content_with_bindings(testcase_template)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
parsed_testcase["url"],
|
parsed_testcase["url"],
|
||||||
|
|||||||
Reference in New Issue
Block a user