mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
add helpers to parse variable and functions
This commit is contained in:
77
ate/utils.py
77
ate/utils.py
@@ -1,3 +1,4 @@
|
||||
import ast
|
||||
import hashlib
|
||||
import json
|
||||
import os.path
|
||||
@@ -15,6 +16,8 @@ except NameError:
|
||||
string_type = str
|
||||
PYTHON_VERSION = 3
|
||||
|
||||
variable_regexp = re.compile(r"^\$(\w+)$")
|
||||
function_regexp = re.compile(r"^\$\{(\w+)\(([\w =,]*)\)\}$")
|
||||
|
||||
def gen_random_string(str_len):
|
||||
return ''.join(
|
||||
@@ -128,6 +131,80 @@ def load_testcases_by_path(path):
|
||||
else:
|
||||
return []
|
||||
|
||||
def is_variable(content):
|
||||
""" check if content is a variable, which is in format $variable
|
||||
@param (str) content
|
||||
@return (bool) True or False
|
||||
|
||||
e.g. $variable => True
|
||||
abc => False
|
||||
"""
|
||||
matched = variable_regexp.match(content)
|
||||
return True if matched else False
|
||||
|
||||
def parse_variable(content):
|
||||
""" parse variable name from string content.
|
||||
@param (str) content
|
||||
@return (str) variable name
|
||||
|
||||
e.g. $variable => variable
|
||||
"""
|
||||
matched = variable_regexp.match(content)
|
||||
return matched.group(1)
|
||||
|
||||
def is_functon(content):
|
||||
""" check if content is a function, which is in format ${func()}
|
||||
@param (str) content
|
||||
@return (bool) True or False
|
||||
|
||||
e.g. ${func()} => True
|
||||
${func(5)} => True
|
||||
${func(1, 2)} => True
|
||||
${func(a=1, b=2)} => True
|
||||
$abc => False
|
||||
abc => False
|
||||
"""
|
||||
matched = function_regexp.match(content)
|
||||
return True if matched else False
|
||||
|
||||
def parse_string_value(str_value):
|
||||
try:
|
||||
return ast.literal_eval(str_value)
|
||||
except ValueError:
|
||||
return str_value
|
||||
|
||||
def parse_function(content):
|
||||
""" parse function name and args from string content.
|
||||
@param (str) content
|
||||
@return (dict) function name and args
|
||||
|
||||
e.g. ${func()} => {'func_name': 'func', 'args': [], 'kwargs': {}}
|
||||
${func(5)} => {'func_name': 'func', 'args': [5], 'kwargs': {}}
|
||||
${func(1, 2)} => {'func_name': 'func', 'args': [1, 2], 'kwargs': {}}
|
||||
${func(a=1, b=2)} => {'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
||||
${func(1, 2, a=3, b=4)} => {'func_name': 'func', 'args': [1, 2], 'kwargs': {'a':3, 'b':4}}
|
||||
"""
|
||||
function_meta = {
|
||||
"args": [],
|
||||
"kwargs": {}
|
||||
}
|
||||
matched = function_regexp.match(content)
|
||||
function_meta["func_name"] = matched.group(1)
|
||||
|
||||
args_str = matched.group(2).replace(" ", "")
|
||||
if args_str == "":
|
||||
return function_meta
|
||||
|
||||
args_list = args_str.split(',')
|
||||
for arg in args_list:
|
||||
if '=' in arg:
|
||||
key, value = arg.split('=')
|
||||
function_meta["kwargs"][key] = parse_string_value(value)
|
||||
else:
|
||||
function_meta["args"].append(parse_string_value(arg))
|
||||
|
||||
return function_meta
|
||||
|
||||
def parse_content_with_variables(content, variables_binds):
|
||||
""" replace variables with bind value
|
||||
"""
|
||||
|
||||
@@ -137,6 +137,90 @@ class TestUtils(ApiServerUnittest):
|
||||
with self.assertRaises(exception.ParamsError):
|
||||
utils.parse_content_with_variables(content, variables_binds)
|
||||
|
||||
def test_is_variable(self):
|
||||
content = "$var"
|
||||
self.assertTrue(utils.is_variable(content))
|
||||
content = "$var123"
|
||||
self.assertTrue(utils.is_variable(content))
|
||||
content = "$var_name"
|
||||
self.assertTrue(utils.is_variable(content))
|
||||
content = "var"
|
||||
self.assertFalse(utils.is_variable(content))
|
||||
content = "a$var"
|
||||
self.assertFalse(utils.is_variable(content))
|
||||
content = "$v ar"
|
||||
self.assertFalse(utils.is_variable(content))
|
||||
content = " "
|
||||
self.assertFalse(utils.is_variable(content))
|
||||
content = "$abc*"
|
||||
self.assertFalse(utils.is_variable(content))
|
||||
|
||||
def test_parse_variable(self):
|
||||
content = "$var"
|
||||
self.assertEqual(utils.parse_variable(content), "var")
|
||||
content = "$var123"
|
||||
self.assertEqual(utils.parse_variable(content), "var123")
|
||||
content = "$var_name"
|
||||
self.assertEqual(utils.parse_variable(content), "var_name")
|
||||
|
||||
def test_is_functon(self):
|
||||
content = "${func()}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(5)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(1, 2)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(a=1, b=2)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func(1, 2, a=3, b=4)}"
|
||||
self.assertTrue(utils.is_functon(content))
|
||||
content = "${func}"
|
||||
self.assertFalse(utils.is_functon(content))
|
||||
content = "$abc"
|
||||
self.assertFalse(utils.is_functon(content))
|
||||
content = "abc"
|
||||
self.assertFalse(utils.is_functon(content))
|
||||
|
||||
def test_parse_string_value(self):
|
||||
str_value = "123"
|
||||
self.assertEqual(utils.parse_string_value(str_value), 123)
|
||||
str_value = "12.3"
|
||||
self.assertEqual(utils.parse_string_value(str_value), 12.3)
|
||||
str_value = "a123"
|
||||
self.assertEqual(utils.parse_string_value(str_value), "a123")
|
||||
|
||||
def test_parse_functon(self):
|
||||
content = "${func()}"
|
||||
self.assertEqual(
|
||||
utils.parse_function(content),
|
||||
{'func_name': 'func', 'args': [], 'kwargs': {}}
|
||||
)
|
||||
content = "${func(5)}"
|
||||
self.assertEqual(
|
||||
utils.parse_function(content),
|
||||
{'func_name': 'func', 'args': [5], 'kwargs': {}}
|
||||
)
|
||||
content = "${func(1, 2)}"
|
||||
self.assertEqual(
|
||||
utils.parse_function(content),
|
||||
{'func_name': 'func', 'args': [1, 2], 'kwargs': {}}
|
||||
)
|
||||
content = "${func(a=1, b=2)}"
|
||||
self.assertEqual(
|
||||
utils.parse_function(content),
|
||||
{'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
||||
)
|
||||
content = "${func(a= 1, b =2)}"
|
||||
self.assertEqual(
|
||||
utils.parse_function(content),
|
||||
{'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
||||
)
|
||||
content = "${func(1, 2, a=3, b=4)}"
|
||||
self.assertEqual(
|
||||
utils.parse_function(content),
|
||||
{'func_name': 'func', 'args': [1, 2], 'kwargs': {'a': 3, 'b': 4}}
|
||||
)
|
||||
|
||||
def test_query_json(self):
|
||||
json_content = {
|
||||
"ids": [1, 2, 3, 4],
|
||||
|
||||
Reference in New Issue
Block a user