mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
bugfix random function in config variables run many times
This commit is contained in:
@@ -506,7 +506,7 @@ def parse_string_variables(content, variables_mapping, functions_mapping):
|
||||
functions_mapping,
|
||||
raise_if_variable_not_found=False
|
||||
)
|
||||
|
||||
variables_mapping[variable_name] = parsed_variable_value
|
||||
# TODO: replace variable label from $var to {{var}}
|
||||
if "${}".format(variable_name) == content:
|
||||
# content is a variable
|
||||
@@ -759,13 +759,15 @@ def __parse_config(config, project_mapping):
|
||||
|
||||
# parse config variables
|
||||
parsed_config_variables = {}
|
||||
for key, value in raw_config_variables_mapping.items():
|
||||
|
||||
for key in raw_config_variables_mapping:
|
||||
parsed_value = parse_data(
|
||||
value,
|
||||
raw_config_variables_mapping[key],
|
||||
raw_config_variables_mapping,
|
||||
functions,
|
||||
raise_if_variable_not_found=False
|
||||
)
|
||||
raw_config_variables_mapping[key] = parsed_value
|
||||
parsed_config_variables[key] = parsed_value
|
||||
|
||||
if parsed_config_variables:
|
||||
@@ -823,12 +825,22 @@ def __parse_testcase_tests(tests, config, project_mapping):
|
||||
test_dict.pop("variables", {}),
|
||||
config_variables
|
||||
)
|
||||
test_dict["variables"] = parse_data(
|
||||
test_dict["variables"],
|
||||
test_dict["variables"],
|
||||
functions,
|
||||
raise_if_variable_not_found=False
|
||||
)
|
||||
|
||||
for key in test_dict["variables"]:
|
||||
parsed_key = parse_data(
|
||||
key,
|
||||
test_dict["variables"],
|
||||
functions,
|
||||
raise_if_variable_not_found=False
|
||||
)
|
||||
parsed_value = parse_data(
|
||||
test_dict["variables"][key],
|
||||
test_dict["variables"],
|
||||
functions,
|
||||
raise_if_variable_not_found=False
|
||||
)
|
||||
if parsed_key in test_dict["variables"]:
|
||||
test_dict["variables"][parsed_key] = parsed_value
|
||||
|
||||
# parse test_dict name
|
||||
test_dict["name"] = parse_data(
|
||||
@@ -974,15 +986,17 @@ def __get_parsed_testsuite_testcases(testcases, testsuite_config, project_mappin
|
||||
|
||||
# parse config variables
|
||||
parsed_config_variables = {}
|
||||
for key, value in parsed_testcase_config_variables.items():
|
||||
|
||||
for key in parsed_testcase_config_variables:
|
||||
try:
|
||||
parsed_value = parse_data(
|
||||
value,
|
||||
parsed_testcase_config_variables[key],
|
||||
parsed_testcase_config_variables,
|
||||
functions
|
||||
)
|
||||
except exceptions.VariableNotFound:
|
||||
pass
|
||||
parsed_testcase_config_variables[key] = parsed_value
|
||||
parsed_config_variables[key] = parsed_value
|
||||
|
||||
if parsed_config_variables:
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
variables:
|
||||
var_a: 0
|
||||
var_c: "${sum_two(1, 2)}"
|
||||
var_d: "${gen_random_string(5)}"
|
||||
var_e: $var_d
|
||||
PROJECT_KEY: ${ENV(PROJECT_KEY)}
|
||||
# parameters:
|
||||
# - "var_a-var_b":
|
||||
|
||||
@@ -598,6 +598,9 @@ class TestApi(ApiServerUnittest):
|
||||
|
||||
testcase1 = parsed_testcases[0]["teststeps"][0]
|
||||
self.assertIn("setup and reset all (override)", testcase1["config"]["name"])
|
||||
self.assertEqual(testcase1["teststeps"][0]["variables"]["var_c"], testcase1["teststeps"][0]["variables"]["var_d"])
|
||||
self.assertEqual(testcase1["teststeps"][0]["variables"]["var_a"], testcase1["teststeps"][0]["variables"]["var_b"])
|
||||
self.assertNotEqual(testcase1["teststeps"][0]["variables"]["var_a"], testcase1["teststeps"][0]["variables"]["var_c"])
|
||||
self.assertNotIn("testcase_def", testcase1)
|
||||
self.assertEqual(len(testcase1["teststeps"]), 2)
|
||||
self.assertEqual(
|
||||
|
||||
@@ -394,8 +394,8 @@ class TestSuiteLoader(unittest.TestCase):
|
||||
testsuites_list[0]["config"]["name"]
|
||||
)
|
||||
self.assertEqual(
|
||||
{'device_sn': '${gen_random_string(15)}'},
|
||||
testsuites_list[0]["config"]["variables"]
|
||||
'${gen_random_string(15)}',
|
||||
testsuites_list[0]["config"]["variables"]['device_sn']
|
||||
)
|
||||
self.assertIn(
|
||||
"create user 1000 and check result.",
|
||||
|
||||
@@ -452,6 +452,7 @@ class TestParser(unittest.TestCase):
|
||||
test_dict1 = parsed_testcases[0]["teststeps"][0]
|
||||
self.assertEqual(test_dict1["variables"]["var_c"], 3)
|
||||
self.assertEqual(test_dict1["variables"]["PROJECT_KEY"], "ABCDEFGH")
|
||||
self.assertEqual(test_dict1["variables"]["var_d"], test_dict1["variables"]["var_e"])
|
||||
# TODO: parameters
|
||||
# self.assertEqual(len(parsed_testcases), 2 * 2)
|
||||
self.assertEqual(parsed_testcases[0]["config"]["name"], '1230')
|
||||
@@ -612,11 +613,12 @@ class TestParser(unittest.TestCase):
|
||||
self.assertEqual(parsed_testcase["num4"], "${sum_two($num0, 5)}")
|
||||
|
||||
def test_parse_tests_variable_with_function(self):
|
||||
from tests.debugtalk import sum_two
|
||||
from tests.debugtalk import sum_two, gen_random_string
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": {
|
||||
"sum_two": sum_two
|
||||
"sum_two": sum_two,
|
||||
"gen_random_string": gen_random_string
|
||||
}
|
||||
},
|
||||
'testcases': [
|
||||
@@ -625,7 +627,9 @@ class TestParser(unittest.TestCase):
|
||||
'name': '',
|
||||
"base_url": "$host1",
|
||||
'variables': {
|
||||
"host1": "https://debugtalk.com"
|
||||
"host1": "https://debugtalk.com",
|
||||
"var_a": "${gen_random_string(5)}",
|
||||
"var_b": "$var_a"
|
||||
}
|
||||
},
|
||||
"teststeps": [
|
||||
@@ -636,7 +640,9 @@ class TestParser(unittest.TestCase):
|
||||
"host2": "https://httprunner.org",
|
||||
"num3": "${sum_two($num2, 4)}",
|
||||
"num2": "${sum_two($num1, 3)}",
|
||||
"num1": "${sum_two(1, 2)}"
|
||||
"num1": "${sum_two(1, 2)}",
|
||||
"str1": "${gen_random_string(5)}",
|
||||
"str2": "$str1"
|
||||
},
|
||||
'request': {
|
||||
'url': '/api1/?num1=$num1&num2=$num2&num3=$num3',
|
||||
@@ -651,6 +657,7 @@ class TestParser(unittest.TestCase):
|
||||
test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0]
|
||||
self.assertEqual(test_dict["variables"]["num3"], 10)
|
||||
self.assertEqual(test_dict["variables"]["num2"], 6)
|
||||
self.assertEqual(test_dict["variables"]["str1"], test_dict["variables"]["str2"])
|
||||
self.assertEqual(
|
||||
test_dict["request"]["url"],
|
||||
"https://httprunner.org/api1/?num1=3&num2=6&num3=10"
|
||||
|
||||
@@ -2,6 +2,8 @@ config:
|
||||
name: create users with uid
|
||||
variables:
|
||||
device_sn: ${gen_random_string(15)}
|
||||
var_a: ${gen_random_string(5)}
|
||||
var_b: $var_a
|
||||
base_url: "http://127.0.0.1:5000"
|
||||
|
||||
testcases:
|
||||
@@ -9,8 +11,12 @@ testcases:
|
||||
testcase: testcases/create_and_check.yml
|
||||
variables:
|
||||
uid: 1000
|
||||
var_c: ${gen_random_string(5)}
|
||||
var_d: $var_c
|
||||
|
||||
create user 1001 and check result.:
|
||||
testcase: testcases/create_and_check.yml
|
||||
variables:
|
||||
uid: 1001
|
||||
var_c: ${gen_random_string(5)}
|
||||
var_d: $var_c
|
||||
|
||||
Reference in New Issue
Block a user