mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
bugfix #9: handle string content with multiple identical variables.
This commit is contained in:
@@ -188,31 +188,3 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
self.assertEqual(len(parsed_request["headers"]["random"]), 5)
|
||||
self.assertIn("data", parsed_request)
|
||||
self.assertEqual(parsed_request["data"], testcase["variable_binds"][2]["data"])
|
||||
|
||||
def test_get_eval_value(self):
|
||||
self.context.testcase_variables_mapping = {
|
||||
"str_1": "str_value1",
|
||||
"str_2": "str_value2"
|
||||
}
|
||||
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_1", "str3"]),
|
||||
["str_value1", "str3"]
|
||||
)
|
||||
self.assertEqual(
|
||||
self.context.get_eval_value({"key": "$str_1"}),
|
||||
{"key": "str_value1"}
|
||||
)
|
||||
|
||||
import random, string
|
||||
self.context.testcase_functions_config["gen_random_string"] = \
|
||||
lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \
|
||||
for _ in range(str_len))
|
||||
result = self.context.get_eval_value("${gen_random_string(5)}")
|
||||
self.assertEqual(len(result), 5)
|
||||
|
||||
add_two_nums = lambda a, b=1: a + b
|
||||
self.context.testcase_functions_config["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)
|
||||
|
||||
@@ -1,120 +1,111 @@
|
||||
import unittest
|
||||
|
||||
from ate.testcase import parse_template, parse_content_with_variables
|
||||
from ate import exception
|
||||
from ate.exception import ParamsError
|
||||
from ate.testcase import parse_content_with_bindings
|
||||
|
||||
|
||||
class TestcaseParserUnittest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.variables_binds = {
|
||||
def test_parse_content_with_bindings_variables(self):
|
||||
variables_binds = {
|
||||
"str_1": "str_value1",
|
||||
"str_2": "str_value2"
|
||||
}
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings("$str_1", variables_binds, {}),
|
||||
"str_value1"
|
||||
)
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings("123$str_1/456", variables_binds, {}),
|
||||
"123str_value1/456"
|
||||
)
|
||||
|
||||
with self.assertRaises(ParamsError):
|
||||
parse_content_with_bindings("$str_3", variables_binds, {})
|
||||
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings(["$str_1", "str3"], variables_binds, {}),
|
||||
["str_value1", "str3"]
|
||||
)
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings({"key": "$str_1"}, variables_binds, {}),
|
||||
{"key": "str_value1"}
|
||||
)
|
||||
|
||||
def test_parse_content_with_bindings_multiple_identical_variables(self):
|
||||
variables_binds = {
|
||||
"userid": 100,
|
||||
"data": 1498
|
||||
}
|
||||
content = "/users/$userid/training/$data?userId=$userid&data=$data"
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings(content, variables_binds, {}),
|
||||
"/users/100/training/1498?userId=100&data=1498"
|
||||
)
|
||||
|
||||
def test_parse_content_with_bindings_functions(self):
|
||||
import random, string
|
||||
functions_binds = {
|
||||
"gen_random_string": lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \
|
||||
for _ in range(str_len))
|
||||
}
|
||||
|
||||
result = parse_content_with_bindings("${gen_random_string(5)}", {}, functions_binds)
|
||||
self.assertEqual(len(result), 5)
|
||||
|
||||
add_two_nums = lambda a, b=1: a + b
|
||||
functions_binds["add_two_nums"] = add_two_nums
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings("${add_two_nums(1)}", {}, functions_binds),
|
||||
2
|
||||
)
|
||||
self.assertEqual(
|
||||
parse_content_with_bindings("${add_two_nums(1, 2)}", {}, functions_binds),
|
||||
3
|
||||
)
|
||||
|
||||
def test_parse_content_with_bindings_testcase(self):
|
||||
variables_binds = {
|
||||
"uid": "1000",
|
||||
"random": "A2dEx",
|
||||
"authorization": "a83de0ff8d2e896dbd8efb81ba14e17d",
|
||||
"json": {
|
||||
"name": "user1",
|
||||
"password": "123456"
|
||||
},
|
||||
"data": {"name": "user", "password": "123456"},
|
||||
"expected_status": 201,
|
||||
"expected_success": True
|
||||
}
|
||||
|
||||
def test_parse_testcase_template(self):
|
||||
functions_binds = {
|
||||
"add_two_nums": lambda a, b=1: a + b
|
||||
}
|
||||
testcase = {
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/$uid",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json",
|
||||
"authorization": "$authorization",
|
||||
"random": "$random"
|
||||
},
|
||||
"body": "$json"
|
||||
"url": "http://127.0.0.1:5000/api/users/$uid",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Content-Type": "application/json",
|
||||
"authorization": "$authorization",
|
||||
"random": "$random",
|
||||
"sum": "${add_two_nums(1, 2)}"
|
||||
},
|
||||
"response": {
|
||||
"status_code": "$expected_status",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
"body": {
|
||||
"success": "$expected_success",
|
||||
"msg": "user created successfully."
|
||||
}
|
||||
}
|
||||
"body": "$data"
|
||||
}
|
||||
parsed_testcase = parse_template(testcase, self.variables_binds)
|
||||
parsed_testcase = parse_content_with_bindings(testcase, variables_binds, functions_binds)
|
||||
|
||||
self.assertEqual(
|
||||
parsed_testcase["request"]["url"],
|
||||
"http://127.0.0.1:5000/api/users/%s" % self.variables_binds["uid"]
|
||||
parsed_testcase["url"],
|
||||
"http://127.0.0.1:5000/api/users/%s" % variables_binds["uid"]
|
||||
)
|
||||
self.assertEqual(
|
||||
parsed_testcase["request"]["headers"]["authorization"],
|
||||
self.variables_binds["authorization"]
|
||||
parsed_testcase["headers"]["authorization"],
|
||||
variables_binds["authorization"]
|
||||
)
|
||||
self.assertEqual(
|
||||
parsed_testcase["request"]["headers"]["random"],
|
||||
self.variables_binds["random"]
|
||||
parsed_testcase["headers"]["random"],
|
||||
variables_binds["random"]
|
||||
)
|
||||
self.assertEqual(
|
||||
parsed_testcase["request"]["body"],
|
||||
self.variables_binds["json"]
|
||||
parsed_testcase["body"],
|
||||
variables_binds["data"]
|
||||
)
|
||||
self.assertEqual(
|
||||
parsed_testcase["response"]["status_code"],
|
||||
self.variables_binds["expected_status"]
|
||||
parsed_testcase["headers"]["sum"],
|
||||
3
|
||||
)
|
||||
self.assertEqual(
|
||||
parsed_testcase["response"]["body"]["success"],
|
||||
self.variables_binds["expected_success"]
|
||||
)
|
||||
|
||||
def test_parse_testcase_template_miss_bind_variable(self):
|
||||
testcase = {
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/$uid",
|
||||
"method": "$method"
|
||||
}
|
||||
}
|
||||
with self.assertRaises(exception.ParamsError):
|
||||
parse_template(testcase, self.variables_binds)
|
||||
|
||||
def test_parse_testcase_with_new_variable_binds(self):
|
||||
testcase = {
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/users/$uid",
|
||||
"method": "$method"
|
||||
}
|
||||
}
|
||||
new_variable_binds = {
|
||||
"method": "GET"
|
||||
}
|
||||
self.variables_binds.update(new_variable_binds)
|
||||
parsed_testcase = parse_template(testcase, self.variables_binds)
|
||||
|
||||
self.assertEqual(
|
||||
parsed_testcase["request"]["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)
|
||||
|
||||
Reference in New Issue
Block a user