mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
strip from extracted function
This commit is contained in:
+11
-10
@@ -5,8 +5,8 @@ import re
|
|||||||
from ate import exception, utils
|
from ate import exception, utils
|
||||||
|
|
||||||
variable_regexp = r"\$([\w_]+)"
|
variable_regexp = r"\$([\w_]+)"
|
||||||
function_regexp = r"\$\{[\w_]+\([\$\w_ =,]*\)\}"
|
function_regexp = r"\$\{([\w_]+\([\$\w_ =,]*\))\}"
|
||||||
function_regexp_compile = re.compile(r"^\$\{([\w_]+)\(([\$\w_ =,]*)\)\}$")
|
function_regexp_compile = re.compile(r"^([\w_]+)\(([\$\w_ =,]*)\)$")
|
||||||
|
|
||||||
|
|
||||||
def extract_variables(content):
|
def extract_variables(content):
|
||||||
@@ -29,11 +29,11 @@ def extract_functions(content):
|
|||||||
@param (str) content
|
@param (str) content
|
||||||
@return (list) functions list
|
@return (list) functions list
|
||||||
|
|
||||||
e.g. ${func(5)} => ["${func(5)}"]
|
e.g. ${func(5)} => ["func(5)"]
|
||||||
${func(a=1, b=2)} => ["${func(a=1, b=2)}"]
|
${func(a=1, b=2)} => ["func(a=1, b=2)"]
|
||||||
/api/1000?_t=${get_timestamp()} => ["get_timestamp()"]
|
/api/1000?_t=${get_timestamp()} => ["get_timestamp()"]
|
||||||
/api/${add(1, 2)} => ["add(1, 2)"]
|
/api/${add(1, 2)} => ["add(1, 2)"]
|
||||||
"/api/${add(1, 2)}?_t=${get_timestamp()}" => ["${add(1, 2)}", "${get_timestamp()}"]
|
"/api/${add(1, 2)}?_t=${get_timestamp()}" => ["add(1, 2)", "get_timestamp()"]
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return re.findall(function_regexp, content)
|
return re.findall(function_regexp, content)
|
||||||
@@ -60,11 +60,11 @@ def parse_function(content):
|
|||||||
@param (str) content
|
@param (str) content
|
||||||
@return (dict) function name and args
|
@return (dict) function name and args
|
||||||
|
|
||||||
e.g. ${func()} => {'func_name': 'func', 'args': [], 'kwargs': {}}
|
e.g. func() => {'func_name': 'func', 'args': [], 'kwargs': {}}
|
||||||
${func(5)} => {'func_name': 'func', 'args': [5], 'kwargs': {}}
|
func(5) => {'func_name': 'func', 'args': [5], 'kwargs': {}}
|
||||||
${func(1, 2)} => {'func_name': 'func', 'args': [1, 2], '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(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}}
|
func(1, 2, a=3, b=4) => {'func_name': 'func', 'args': [1, 2], 'kwargs': {'a':3, 'b':4}}
|
||||||
"""
|
"""
|
||||||
function_meta = {
|
function_meta = {
|
||||||
"args": [],
|
"args": [],
|
||||||
@@ -147,6 +147,7 @@ class TestcaseParser(object):
|
|||||||
kwargs = self.parse_content_with_bindings(kwargs)
|
kwargs = self.parse_content_with_bindings(kwargs)
|
||||||
eval_value = func(*args, **kwargs)
|
eval_value = func(*args, **kwargs)
|
||||||
|
|
||||||
|
func_content = "${" + func_content + "}"
|
||||||
if func_content == content:
|
if func_content == content:
|
||||||
# content is a variable
|
# content is a variable
|
||||||
content = eval_value
|
content = eval_value
|
||||||
|
|||||||
+14
-14
@@ -131,27 +131,27 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_parse_function(self):
|
def test_parse_function(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_function("${func()}"),
|
testcase.parse_function("func()"),
|
||||||
{'func_name': 'func', 'args': [], 'kwargs': {}}
|
{'func_name': 'func', 'args': [], 'kwargs': {}}
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_function("${func(5)}"),
|
testcase.parse_function("func(5)"),
|
||||||
{'func_name': 'func', 'args': [5], 'kwargs': {}}
|
{'func_name': 'func', 'args': [5], 'kwargs': {}}
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_function("${func(1, 2)}"),
|
testcase.parse_function("func(1, 2)"),
|
||||||
{'func_name': 'func', 'args': [1, 2], 'kwargs': {}}
|
{'func_name': 'func', 'args': [1, 2], 'kwargs': {}}
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_function("${func(a=1, b=2)}"),
|
testcase.parse_function("func(a=1, b=2)"),
|
||||||
{'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
{'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_function("${func(a= 1, b =2)}"),
|
testcase.parse_function("func(a= 1, b =2)"),
|
||||||
{'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
{'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.parse_function("${func(1, 2, a=3, b=4)}"),
|
testcase.parse_function("func(1, 2, a=3, b=4)"),
|
||||||
{'func_name': 'func', 'args': [1, 2], 'kwargs': {'a': 3, 'b': 4}}
|
{'func_name': 'func', 'args': [1, 2], 'kwargs': {'a': 3, 'b': 4}}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -232,35 +232,35 @@ class TestcaseParserUnittest(unittest.TestCase):
|
|||||||
def test_extract_functions(self):
|
def test_extract_functions(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("${func()}"),
|
testcase.extract_functions("${func()}"),
|
||||||
["${func()}"]
|
["func()"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("${func(5)}"),
|
testcase.extract_functions("${func(5)}"),
|
||||||
["${func(5)}"]
|
["func(5)"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("${func(a=1, b=2)}"),
|
testcase.extract_functions("${func(a=1, b=2)}"),
|
||||||
["${func(a=1, b=2)}"]
|
["func(a=1, b=2)"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("${func(1, $b, c=$x, d=4)}"),
|
testcase.extract_functions("${func(1, $b, c=$x, d=4)}"),
|
||||||
["${func(1, $b, c=$x, d=4)}"]
|
["func(1, $b, c=$x, d=4)"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("/api/1000?_t=${get_timestamp()}"),
|
testcase.extract_functions("/api/1000?_t=${get_timestamp()}"),
|
||||||
["${get_timestamp()}"]
|
["get_timestamp()"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("/api/${add(1, 2)}"),
|
testcase.extract_functions("/api/${add(1, 2)}"),
|
||||||
["${add(1, 2)}"]
|
["add(1, 2)"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("/api/${add(1, 2)}?_t=${get_timestamp()}"),
|
testcase.extract_functions("/api/${add(1, 2)}?_t=${get_timestamp()}"),
|
||||||
["${add(1, 2)}", "${get_timestamp()}"]
|
["add(1, 2)", "get_timestamp()"]
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
testcase.extract_functions("abc${func(1, 2, a=3, b=4)}def"),
|
testcase.extract_functions("abc${func(1, 2, a=3, b=4)}def"),
|
||||||
["${func(1, 2, a=3, b=4)}"]
|
["func(1, 2, a=3, b=4)"]
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_eval_content_functions(self):
|
def test_eval_content_functions(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user