mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 08:19:45 +08:00
change variable marker and function marker:
1, variable marker: ${var} => $var;
2, function marker: {'func': 'gen_random_string', 'args': [5]} => ${gen_random_string(5).
This commit is contained in:
@@ -78,9 +78,9 @@ class Context(object):
|
||||
e.g.
|
||||
[
|
||||
{"TOKEN": "debugtalk"},
|
||||
{"random": {"func": "gen_random_string", "args": [5]}},
|
||||
{"random": "${gen_random_string(5)}"},
|
||||
{"json": {'name': 'user', 'password': '123456'}},
|
||||
{"md5": {"func": "gen_md5", "args": ["${TOKEN}", "${json}", "${random}"]}}
|
||||
{"md5": "${gen_md5($TOKEN, $json, $random)}"}
|
||||
]
|
||||
"""
|
||||
if level == "testset":
|
||||
@@ -148,29 +148,39 @@ class Context(object):
|
||||
|
||||
def get_eval_value(self, data):
|
||||
""" evaluate data recursively, each variable in data will be evaluated.
|
||||
variables marker: ${variable}.
|
||||
"""
|
||||
if isinstance(data, str):
|
||||
return utils.parse_content_with_variables(data, self.testcase_variables_mapping)
|
||||
|
||||
if isinstance(data, list):
|
||||
if isinstance(data, (list, tuple)):
|
||||
return [self.get_eval_value(item) for item in data]
|
||||
|
||||
if isinstance(data, dict):
|
||||
if "func" in data:
|
||||
# this is a function, e.g. {"func": "gen_random_string", "args": [5]}
|
||||
# function marker: "func" key in dict
|
||||
# the function will be called, and its return value will be binded
|
||||
# to the testcase context variable.
|
||||
func_name = data['func']
|
||||
args = self.get_eval_value(data.get('args', []))
|
||||
kargs = self.get_eval_value(data.get('kargs', {}))
|
||||
return self.testcase_config["functions"][func_name](*args, **kargs)
|
||||
else:
|
||||
evaluated_data = {}
|
||||
for key, value in data.items():
|
||||
evaluated_data[key] = self.get_eval_value(value)
|
||||
evaluated_data = {}
|
||||
for key, value in data.items():
|
||||
evaluated_data[key] = self.get_eval_value(value)
|
||||
|
||||
return evaluated_data
|
||||
return evaluated_data
|
||||
|
||||
return data
|
||||
if isinstance(data, (int, float)):
|
||||
return data
|
||||
|
||||
# data is in string format here
|
||||
data = data.strip()
|
||||
if utils.is_variable(data):
|
||||
# variable marker: $var
|
||||
variable_name = utils.parse_variable(data)
|
||||
value = self.testcase_variables_mapping.get(variable_name)
|
||||
if value is None:
|
||||
raise exception.ParamsError(
|
||||
"%s is not defined in bind variables!" % variable_name)
|
||||
return value
|
||||
|
||||
elif utils.is_functon(data):
|
||||
# function marker: ${func(1, 2, a=3, b=4)}
|
||||
fuction_meta = utils.parse_function(data)
|
||||
func_name = fuction_meta['func_name']
|
||||
args = fuction_meta.get('args', [])
|
||||
kargs = fuction_meta.get('kargs', {})
|
||||
args = self.get_eval_value(args)
|
||||
kargs = self.get_eval_value(kargs)
|
||||
return self.testcase_config["functions"][func_name](*args, **kargs)
|
||||
else:
|
||||
return data
|
||||
|
||||
@@ -26,7 +26,7 @@ class Runner(object):
|
||||
"import_module_functions": ["test.data.custom_functions"],
|
||||
"variable_binds": [
|
||||
{"TOKEN": "debugtalk"},
|
||||
{"random": {"func": "gen_random_string", "args": [5]}},
|
||||
{"random": "${gen_random_string(5)}"},
|
||||
]
|
||||
}
|
||||
@param (str) context level, testcase or testset
|
||||
@@ -62,8 +62,8 @@ class Runner(object):
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json",
|
||||
"authorization": "${authorization}",
|
||||
"random": "${random}"
|
||||
"authorization": "$authorization",
|
||||
"random": "$random"
|
||||
},
|
||||
"body": '{"name": "user", "password": "123456"}'
|
||||
},
|
||||
|
||||
@@ -1,19 +1,40 @@
|
||||
from ate import utils
|
||||
import re
|
||||
from ate.exception import ParamsError
|
||||
|
||||
|
||||
def parse_content_with_variables(content, variables_binds):
|
||||
""" replace variables with bind value
|
||||
"""
|
||||
# check if content includes $variable
|
||||
matched = re.match(r"^(.*)\$(\w+)(.*)$", content)
|
||||
if matched:
|
||||
# this is a variable, and will replace with its bind value
|
||||
variable_name = matched.group(2)
|
||||
value = variables_binds.get(variable_name)
|
||||
if value is None:
|
||||
raise ParamsError(
|
||||
"%s is not defined in bind variables!" % variable_name)
|
||||
if matched.group(1) or matched.group(3):
|
||||
# e.g. /api/users/$uid
|
||||
return content.replace("$%s" % variable_name, value)
|
||||
|
||||
return value
|
||||
|
||||
return content
|
||||
|
||||
def parse_template(testcase_template, variables_binds):
|
||||
""" parse testcase_template, replace all variables with bind value.
|
||||
variables marker: ${variable}.
|
||||
variables marker: $variable.
|
||||
@param (dict) testcase_template
|
||||
{
|
||||
"url": "http://127.0.0.1:5000/api/users/${uid}",
|
||||
"url": "http://127.0.0.1:5000/api/users/$uid",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json",
|
||||
"authorization": "${authorization}",
|
||||
"random": "${random}"
|
||||
"authorization": "$authorization",
|
||||
"random": "$random"
|
||||
},
|
||||
"body": "${data}"
|
||||
"body": "$data"
|
||||
}
|
||||
@param (dict) variables binds mapping
|
||||
{
|
||||
@@ -36,10 +57,9 @@ def parse_template(testcase_template, variables_binds):
|
||||
|
||||
def substitute(content):
|
||||
""" substitute content recursively, each variable will be replaced with bind value.
|
||||
variables marker: ${variable}.
|
||||
"""
|
||||
if isinstance(content, str):
|
||||
return utils.parse_content_with_variables(content, variables_binds)
|
||||
return parse_content_with_variables(content, variables_binds)
|
||||
|
||||
if isinstance(content, list):
|
||||
return [substitute(item) for item in content]
|
||||
|
||||
20
ate/utils.py
20
ate/utils.py
@@ -214,26 +214,6 @@ def parse_function(content):
|
||||
|
||||
return function_meta
|
||||
|
||||
def parse_content_with_variables(content, variables_binds):
|
||||
""" replace variables with bind value
|
||||
"""
|
||||
# check if content includes ${variable}
|
||||
matched = re.match(r"(.*)\$\{(.*)\}(.*)", content)
|
||||
if matched:
|
||||
# this is a variable, and will replace with its bind value
|
||||
variable_name = matched.group(2)
|
||||
value = variables_binds.get(variable_name)
|
||||
if value is None:
|
||||
raise ParamsError(
|
||||
"%s is not defined in bind variables!" % variable_name)
|
||||
if matched.group(1) or matched.group(3):
|
||||
# e.g. /api/users/${uid}
|
||||
return re.sub(r"\$\{.*\}", value, content)
|
||||
|
||||
return value
|
||||
|
||||
return content
|
||||
|
||||
def query_json(json_content, query, delimiter='.'):
|
||||
""" Do an xpath-like query with json_content.
|
||||
@param (json_content) json_content
|
||||
|
||||
Reference in New Issue
Block a user