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:
httprunner
2017-07-04 16:45:18 +08:00
parent d941f3a3d5
commit 17bf07f012
11 changed files with 152 additions and 138 deletions
+27 -17
View File
@@ -78,9 +78,9 @@ class Context(object):
e.g. e.g.
[ [
{"TOKEN": "debugtalk"}, {"TOKEN": "debugtalk"},
{"random": {"func": "gen_random_string", "args": [5]}}, {"random": "${gen_random_string(5)}"},
{"json": {'name': 'user', 'password': '123456'}}, {"json": {'name': 'user', 'password': '123456'}},
{"md5": {"func": "gen_md5", "args": ["${TOKEN}", "${json}", "${random}"]}} {"md5": "${gen_md5($TOKEN, $json, $random)}"}
] ]
""" """
if level == "testset": if level == "testset":
@@ -148,29 +148,39 @@ class Context(object):
def get_eval_value(self, data): def get_eval_value(self, data):
""" evaluate data recursively, each variable in data will be evaluated. """ evaluate data recursively, each variable in data will be evaluated.
variables marker: ${variable}.
""" """
if isinstance(data, str): if isinstance(data, (list, tuple)):
return utils.parse_content_with_variables(data, self.testcase_variables_mapping)
if isinstance(data, list):
return [self.get_eval_value(item) for item in data] return [self.get_eval_value(item) for item in data]
if isinstance(data, dict): 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 = {} evaluated_data = {}
for key, value in data.items(): for key, value in data.items():
evaluated_data[key] = self.get_eval_value(value) evaluated_data[key] = self.get_eval_value(value)
return evaluated_data return evaluated_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 return data
+3 -3
View File
@@ -26,7 +26,7 @@ class Runner(object):
"import_module_functions": ["test.data.custom_functions"], "import_module_functions": ["test.data.custom_functions"],
"variable_binds": [ "variable_binds": [
{"TOKEN": "debugtalk"}, {"TOKEN": "debugtalk"},
{"random": {"func": "gen_random_string", "args": [5]}}, {"random": "${gen_random_string(5)}"},
] ]
} }
@param (str) context level, testcase or testset @param (str) context level, testcase or testset
@@ -62,8 +62,8 @@ class Runner(object):
"method": "POST", "method": "POST",
"headers": { "headers": {
"Content-Type": "application/json", "Content-Type": "application/json",
"authorization": "${authorization}", "authorization": "$authorization",
"random": "${random}" "random": "$random"
}, },
"body": '{"name": "user", "password": "123456"}' "body": '{"name": "user", "password": "123456"}'
}, },
+28 -8
View File
@@ -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): def parse_template(testcase_template, variables_binds):
""" parse testcase_template, replace all variables with bind value. """ parse testcase_template, replace all variables with bind value.
variables marker: ${variable}. variables marker: $variable.
@param (dict) testcase_template @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", "method": "POST",
"headers": { "headers": {
"Content-Type": "application/json", "Content-Type": "application/json",
"authorization": "${authorization}", "authorization": "$authorization",
"random": "${random}" "random": "$random"
}, },
"body": "${data}" "body": "$data"
} }
@param (dict) variables binds mapping @param (dict) variables binds mapping
{ {
@@ -36,10 +57,9 @@ def parse_template(testcase_template, variables_binds):
def substitute(content): def substitute(content):
""" substitute content recursively, each variable will be replaced with bind value. """ substitute content recursively, each variable will be replaced with bind value.
variables marker: ${variable}.
""" """
if isinstance(content, str): 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): if isinstance(content, list):
return [substitute(item) for item in content] return [substitute(item) for item in content]
-20
View File
@@ -214,26 +214,6 @@ def parse_function(content):
return function_meta 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='.'): def query_json(json_content, query, delimiter='.'):
""" Do an xpath-like query with json_content. """ Do an xpath-like query with json_content.
@param (json_content) json_content @param (json_content) json_content
+7 -7
View File
@@ -7,15 +7,15 @@ register_variables:
register_template_variables: register_template_variables:
variable_binds: variable_binds:
- TOKEN: "debugtalk" - TOKEN: "debugtalk"
- token: ${TOKEN} - token: $TOKEN
bind_lambda_functions: bind_lambda_functions:
function_binds: function_binds:
add_one: "lambda x: x + 1" add_one: "lambda x: x + 1"
add_two_nums: "lambda x, y: x + y" add_two_nums: "lambda x, y: x + y"
variable_binds: variable_binds:
- add1: {"func": "add_one", "args": [2]} - add1: ${add_one(2)}
- sum2nums: {"func": "add_two_nums", "args": [2, 3]} - sum2nums: ${add_two_nums(2, 3)}
bind_lambda_functions_with_import: bind_lambda_functions_with_import:
requires: requires:
@@ -27,9 +27,9 @@ bind_lambda_functions_with_import:
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: ${gen_random_string(5)}
- data: "{'name': 'user', 'password': '123456'}" - data: "{'name': 'user', 'password': '123456'}"
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - authorization: ${gen_md5($TOKEN, $data, $random)}
bind_module_functions: bind_module_functions:
function_binds: function_binds:
@@ -37,6 +37,6 @@ bind_module_functions:
- test.data.custom_functions - test.data.custom_functions
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: ${gen_random_string(5)}
- data: "{'name': 'user', 'password': '123456'}" - data: "{'name': 'user', 'password': '123456'}"
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - authorization: ${gen_md5($TOKEN, $data, $random)}
+9 -9
View File
@@ -4,9 +4,9 @@
- test.data.custom_functions - test.data.custom_functions
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- json: {"name": "user", "password": "123456"} - json: {}
- random: {"func": "gen_random_string", "args": [5]} - random: ${gen_random_string(5)}
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${json}", "${random}"]} - authorization: ${gen_md5($TOKEN, $json, $random)}
- test: - test:
name: create user which does not exist name: create user which does not exist
@@ -17,9 +17,9 @@
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: $authorization
random: "${random}" random: $random
json: "${json}" json: $json
validators: validators:
- {"check": "status_code", "comparator": "eq", "expected": 201} - {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true} - {"check": "content.success", "comparator": "eq", "expected": true}
@@ -33,9 +33,9 @@
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: $authorization
random: "${random}" random: $random
json: "${json}" json: $json
validators: validators:
- {"check": "status_code", "comparator": "eq", "expected": 500} - {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false} - {"check": "content.success", "comparator": "eq", "expected": false}
+10 -10
View File
@@ -10,17 +10,17 @@
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: ${gen_random_string(5)}
- data: '{"name": "user", "password": "123456"}' - data: '{"name": "user", "password": "123456"}'
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - authorization: ${gen_md5($TOKEN, $data, $random)}
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: $authorization
random: "${random}" random: $random
data: "${data}" data: $data
validators: validators:
- {"check": "status_code", "comparator": "eq", "expected": 201} - {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true} - {"check": "content.success", "comparator": "eq", "expected": true}
@@ -36,17 +36,17 @@
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()" gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- random: {"func": "gen_random_string", "args": [5]} - random: ${gen_random_string(5)}
- data: '{"name": "user", "password": "123456"}' - data: '{"name": "user", "password": "123456"}'
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - authorization: ${gen_md5($TOKEN, $data, $random)}
request: request:
url: http://127.0.0.1:5000/api/users/1000 url: http://127.0.0.1:5000/api/users/1000
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: $authorization
random: "${random}" random: $random
data: "${data}" data: $data
validators: validators:
- {"check": "status_code", "comparator": "eq", "expected": 500} - {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false} - {"check": "content.success", "comparator": "eq", "expected": false}
+8 -8
View File
@@ -10,8 +10,8 @@
variable_binds: variable_binds:
- TOKEN: debugtalk - TOKEN: debugtalk
- data: "" - data: ""
- random: {"func": "gen_random_string", "args": [5]} - random: ${gen_random_string(5)}
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - authorization: ${gen_md5($TOKEN, $data, $random)}
request: request:
base_url: http://127.0.0.1:5000 base_url: http://127.0.0.1:5000
@@ -24,9 +24,9 @@
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: $authorization
random: "${random}" random: $random
data: "${data}" data: $data
validators: validators:
- {"check": "status_code", "comparator": "eq", "expected": 201} - {"check": "status_code", "comparator": "eq", "expected": 201}
- {"check": "content.success", "comparator": "eq", "expected": true} - {"check": "content.success", "comparator": "eq", "expected": true}
@@ -41,9 +41,9 @@
method: POST method: POST
headers: headers:
Content-Type: application/json Content-Type: application/json
authorization: "${authorization}" authorization: $authorization
random: "${random}" random: $random
data: "${data}" data: $data
validators: validators:
- {"check": "status_code", "comparator": "eq", "expected": 500} - {"check": "status_code", "comparator": "eq", "expected": 500}
- {"check": "content.success", "comparator": "eq", "expected": false} - {"check": "content.success", "comparator": "eq", "expected": false}
+22 -18
View File
@@ -43,7 +43,7 @@ class VariableBindsUnittest(unittest.TestCase):
testcase1 = { testcase1 = {
"variable_binds": [ "variable_binds": [
{"GLOBAL_TOKEN": "debugtalk"}, {"GLOBAL_TOKEN": "debugtalk"},
{"token": "${GLOBAL_TOKEN}"} {"token": "$GLOBAL_TOKEN"}
] ]
} }
testcase2 = self.testcases["register_template_variables"] testcase2 = self.testcases["register_template_variables"]
@@ -65,8 +65,8 @@ class VariableBindsUnittest(unittest.TestCase):
"add_two_nums": lambda x, y: x + y "add_two_nums": lambda x, y: x + y
}, },
"variable_binds": [ "variable_binds": [
{"add1": {"func": "add_one", "args": [2]}}, {"add1": "${add_one(2)}"},
{"sum2nums": {"func": "add_two_nums", "args": [2, 3]}} {"sum2nums": "${add_two_nums(2,3)}"}
] ]
} }
testcase2 = self.testcases["bind_lambda_functions"] testcase2 = self.testcases["bind_lambda_functions"]
@@ -93,9 +93,9 @@ class VariableBindsUnittest(unittest.TestCase):
}, },
"variable_binds": [ "variable_binds": [
{"TOKEN": "debugtalk"}, {"TOKEN": "debugtalk"},
{"random": {"func": "gen_random_string", "args": [5]}}, {"random": "${gen_random_string(5)}"},
{"data": '{"name": "user", "password": "123456"}'}, {"data": '{"name": "user", "password": "123456"}'},
{"authorization": {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]}} {"authorization": "${gen_md5($TOKEN, $data, $random)}"}
] ]
} }
testcase2 = self.testcases["bind_lambda_functions_with_import"] testcase2 = self.testcases["bind_lambda_functions_with_import"]
@@ -130,9 +130,9 @@ class VariableBindsUnittest(unittest.TestCase):
"import_module_functions": ["test.data.custom_functions"], "import_module_functions": ["test.data.custom_functions"],
"variable_binds": [ "variable_binds": [
{"TOKEN": "debugtalk"}, {"TOKEN": "debugtalk"},
{"random": {"func": "gen_random_string", "args": [5]}}, {"random": "${gen_random_string(5)}"},
{"data": '{"name": "user", "password": "123456"}'}, {"data": '{"name": "user", "password": "123456"}'},
{"authorization": {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]}} {"authorization": "${gen_md5($TOKEN, $data, $random)}"}
] ]
} }
testcase2 = self.testcases["bind_module_functions"] testcase2 = self.testcases["bind_module_functions"]
@@ -165,19 +165,19 @@ class VariableBindsUnittest(unittest.TestCase):
"import_module_functions": ["test.data.custom_functions"], "import_module_functions": ["test.data.custom_functions"],
"variable_binds": [ "variable_binds": [
{"TOKEN": "debugtalk"}, {"TOKEN": "debugtalk"},
{"random": {"func": "gen_random_string", "args": [5]}}, {"random": "${gen_random_string(5)}"},
{"data": '{"name": "user", "password": "123456"}'}, {"data": '{"name": "user", "password": "123456"}'},
{"authorization": {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]}} {"authorization": "${gen_md5($TOKEN, $data, $random)}"}
], ],
"request": { "request": {
"url": "http://127.0.0.1:5000/api/users/1000", "url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST", "method": "POST",
"headers": { "headers": {
"Content-Type": "application/json", "Content-Type": "application/json",
"authorization": "${authorization}", "authorization": "$authorization",
"random": "${random}" "random": "$random"
}, },
"data": "${data}" "data": "$data"
} }
} }
test_runner.init_config(testcase, level="testcase") test_runner.init_config(testcase, level="testcase")
@@ -194,14 +194,14 @@ class VariableBindsUnittest(unittest.TestCase):
"str_1": "str_value1", "str_1": "str_value1",
"str_2": "str_value2" "str_2": "str_value2"
} }
self.assertEqual(self.context.get_eval_value("${str_1}"), "str_value1") self.assertEqual(self.context.get_eval_value("$str_1"), "str_value1")
self.assertEqual(self.context.get_eval_value("${str_2}"), "str_value2") self.assertEqual(self.context.get_eval_value("$str_2"), "str_value2")
self.assertEqual( self.assertEqual(
self.context.get_eval_value(["${str_1}", "str3"]), self.context.get_eval_value(["$str_1", "str3"]),
["str_value1", "str3"] ["str_value1", "str3"]
) )
self.assertEqual( self.assertEqual(
self.context.get_eval_value({"key": "${str_1}"}), self.context.get_eval_value({"key": "$str_1"}),
{"key": "str_value1"} {"key": "str_value1"}
) )
@@ -209,6 +209,10 @@ class VariableBindsUnittest(unittest.TestCase):
self.context.testcase_config["functions"]["gen_random_string"] = \ self.context.testcase_config["functions"]["gen_random_string"] = \
lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \ lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \
for _ in range(str_len)) for _ in range(str_len))
result = self.context.get_eval_value( result = self.context.get_eval_value("${gen_random_string(5)}")
{"func": "gen_random_string", "args": [5]})
self.assertEqual(len(result), 5) self.assertEqual(len(result), 5)
add_two_nums = lambda a, b=1: a + b
self.context.testcase_config["functions"]["add_two_nums"] = add_two_nums
self.assertEqual(self.context.get_eval_value("${add_two_nums(1)}"), 2)
self.assertEqual(self.context.get_eval_value("${add_two_nums(1, 2)}"), 3)
+33 -11
View File
@@ -1,6 +1,6 @@
import unittest import unittest
from ate.testcase import parse_template from ate.testcase import parse_template, parse_content_with_variables
from ate import exception from ate import exception
@@ -22,22 +22,22 @@ class TestcaseParserUnittest(unittest.TestCase):
def test_parse_testcase_template(self): def test_parse_testcase_template(self):
testcase = { testcase = {
"request": { "request": {
"url": "http://127.0.0.1:5000/api/users/${uid}", "url": "http://127.0.0.1:5000/api/users/$uid",
"method": "POST", "method": "POST",
"headers": { "headers": {
"Content-Type": "application/json", "Content-Type": "application/json",
"authorization": "${authorization}", "authorization": "$authorization",
"random": "${random}" "random": "$random"
}, },
"body": "${json}" "body": "$json"
}, },
"response": { "response": {
"status_code": "${expected_status}", "status_code": "$expected_status",
"headers": { "headers": {
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
"body": { "body": {
"success": "${expected_success}", "success": "$expected_success",
"msg": "user created successfully." "msg": "user created successfully."
} }
} }
@@ -72,8 +72,8 @@ class TestcaseParserUnittest(unittest.TestCase):
def test_parse_testcase_template_miss_bind_variable(self): def test_parse_testcase_template_miss_bind_variable(self):
testcase = { testcase = {
"request": { "request": {
"url": "http://127.0.0.1:5000/api/users/${uid}", "url": "http://127.0.0.1:5000/api/users/$uid",
"method": "${method}" "method": "$method"
} }
} }
with self.assertRaises(exception.ParamsError): with self.assertRaises(exception.ParamsError):
@@ -82,8 +82,8 @@ class TestcaseParserUnittest(unittest.TestCase):
def test_parse_testcase_with_new_variable_binds(self): def test_parse_testcase_with_new_variable_binds(self):
testcase = { testcase = {
"request": { "request": {
"url": "http://127.0.0.1:5000/api/users/${uid}", "url": "http://127.0.0.1:5000/api/users/$uid",
"method": "${method}" "method": "$method"
} }
} }
new_variable_binds = { new_variable_binds = {
@@ -96,3 +96,25 @@ class TestcaseParserUnittest(unittest.TestCase):
parsed_testcase["request"]["method"], parsed_testcase["request"]["method"],
new_variable_binds["method"] new_variable_binds["method"]
) )
def test_parse_content_with_variables(self):
content = "$var"
variables_binds = {
"var": "abc"
}
result = parse_content_with_variables(content, variables_binds)
self.assertEqual(result, "abc")
content = "123$var/456"
variables_binds = {
"var": "abc"
}
result = parse_content_with_variables(content, variables_binds)
self.assertEqual(result, "123abc/456")
content = "$var1"
variables_binds = {
"var2": "abc"
}
with self.assertRaises(exception.ParamsError):
parse_content_with_variables(content, variables_binds)
-22
View File
@@ -115,28 +115,6 @@ class TestUtils(ApiServerUnittest):
testset_list_3 = utils.load_testcases_by_path(path) testset_list_3 = utils.load_testcases_by_path(path)
self.assertEqual(testset_list_3, []) self.assertEqual(testset_list_3, [])
def test_parse_content_with_variables(self):
content = "${var}"
variables_binds = {
"var": "abc"
}
result = utils.parse_content_with_variables(content, variables_binds)
self.assertEqual(result, "abc")
content = "123${var}456"
variables_binds = {
"var": "abc"
}
result = utils.parse_content_with_variables(content, variables_binds)
self.assertEqual(result, "123abc456")
content = "${var1}"
variables_binds = {
"var2": "abc"
}
with self.assertRaises(exception.ParamsError):
utils.parse_content_with_variables(content, variables_binds)
def test_is_variable(self): def test_is_variable(self):
content = "$var" content = "$var"
self.assertTrue(utils.is_variable(content)) self.assertTrue(utils.is_variable(content))