#106: support builtin functions

This commit is contained in:
debugtalk
2018-02-28 23:56:10 +08:00
parent e91fef2595
commit f1cac82a15
4 changed files with 31 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
__title__ = 'HttpRunner'
__description__ = 'HTTP test runner, not just about api test and load test.'
__url__ = 'https://github.com/HttpRunner/HttpRunner'
__version__ = '0.9.7'
__version__ = '0.9.8'
__author__ = 'debugtalk'
__author_email__ = 'mail@debugtalk.com'
__license__ = 'MIT'

View File

@@ -703,6 +703,11 @@ class TestcaseParser(object):
if item_type == "function":
if item_name in self.functions:
return self.functions[item_name]
try:
return eval(item_name)
except NameError:
pass
elif item_type == "variable":
if item_name in self.variables:
return self.variables[item_name]

View File

@@ -34,3 +34,9 @@ bind_module_functions:
- random: ${gen_random_string(5)}
- data: "{'name': 'user', 'password': '123456'}"
- authorization: ${gen_md5($TOKEN, $data, $random)}
builtin_functions:
variables:
- length: ${len(debugtalk)}
- smallest: ${min(2, 3, 8)}
- largest: ${max(2, 3, 8)}

View File

@@ -100,6 +100,25 @@ class VariableBindsUnittest(ApiServerUnittest):
self.assertIn("sum2nums", context_variables)
self.assertEqual(context_variables["sum2nums"], 5)
def test_call_builtin_functions(self):
testcase1 = {
"variables": [
{"length": "${len(debugtalk)}"},
{"smallest": "${min(2, 3, 8)}"},
{"largest": "${max(2, 3, 8)}"}
]
}
testcase2 = self.testcases["builtin_functions"]
for testcase in [testcase1, testcase2]:
variables = testcase['variables']
self.context.bind_variables(variables)
context_variables = self.context.testcase_variables_mapping
self.assertEqual(context_variables["length"], 9)
self.assertEqual(context_variables["smallest"], 2)
self.assertEqual(context_variables["largest"], 8)
def test_context_bind_lambda_functions_with_import(self):
testcase1 = {
"requires": ["random", "string", "hashlib"],