Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"num1": "${sum_two(1, 2)}",
"str1": "${gen_random_string(5)}",
"str2": "$str1"
},
'request': {
'url': '/api1/?num1=$num1&num2=$num2&num3=$num3',
'method': 'GET'
}
}
]
}
]
}
parsed_testcases = parser.parse_tests(tests_mapping)
test_dict = parsed_testcases[0]["teststeps"][0]
variables = parser.parse_variables_mapping(test_dict["variables"])
self.assertEqual(variables["num3"], 10)
self.assertEqual(variables["num2"], 6)
parsed_test_dict = parser.parse_lazy_data(test_dict, variables)
self.assertEqual(parsed_test_dict["base_url"], "https://httprunner.org")
self.assertEqual(
parsed_test_dict["request"]["url"],
"/api1/?num1=3&num2=6&num3=10"
)
self.assertEqual(variables["str1"], variables["str2"])
def test_parse_variables_mapping_ref_self(self):
variables = {
"varC": "${sum_two($a, $b)}",
"a": 1,
"b": 2,
"token": "$token"
}
functions = {
"sum_two": sum_two
}
prepared_variables = parser.prepare_lazy_data(variables, functions, variables.keys())
with self.assertRaises(exceptions.VariableNotFound):
parser.parse_variables_mapping(prepared_variables)
"var2": "abc$$$num0",
"var3": "abc$$$$num0",
}
functions = {
"sum_two": sum_two
}
prepared_variables = parser.prepare_lazy_data(
variables,
functions,
variables.keys()
)
self.assertEqual(prepared_variables["var1"], "abc$num0")
self.assertIsInstance(prepared_variables["var2"], parser.LazyString)
self.assertEqual(prepared_variables["var3"], "abc$$num0")
parsed_variables = parser.parse_variables_mapping(prepared_variables)
self.assertEqual(parsed_variables["var1"], "abc$num0")
self.assertEqual(parsed_variables["var2"], "abc$123")
self.assertEqual(parsed_variables["var3"], "abc$$num0")
def test_get_parsed_request(self):
variables = {
"random": "${gen_random_string(5)}",
"data": '{"name": "user", "password": "123456"}',
"authorization": "${gen_md5($TOKEN, $data, $random)}",
"TOKEN": "debugtalk"
}
functions = {
"gen_random_string": gen_random_string,
"gen_md5": gen_md5
}
variables = parser.prepare_lazy_data(variables, functions, variables.keys())
variables = parser.parse_variables_mapping(variables)
self.context.init_test_variables(variables)
request = {
"url": "http://127.0.0.1:5000/api/users/1000",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"authorization": "$authorization",
"random": "$random",
"secret_key": "$SECRET_KEY"
},
"data": "$data"
}
prepared_request = parser.prepare_lazy_data(
request,
functions,
def __parse_config(self, config: TConfig) -> NoReturn:
config.variables.update(self.__session_variables)
config.variables = parse_variables_mapping(
config.variables, self.__project_meta.functions
)
config.name = parse_data(
config.name, config.variables, self.__project_meta.functions
)
config.base_url = parse_data(
config.base_url, config.variables, self.__project_meta.functions
)
variables_mapping will be evaluated first.
Args:
variables_mapping (dict)
{
"random": "${gen_random_string(5)}",
"authorization": "${gen_md5($TOKEN, $data, $random)}",
"data": '{"name": "user", "password": "123456"}',
"TOKEN": "debugtalk",
}
"""
variables_mapping = copy.deepcopy(variables_mapping or {})
variables_mapping = utils.ensure_mapping_format(variables_mapping)
variables_mapping.update(self.session_variables_mapping)
parsed_variables_mapping = parser.parse_variables_mapping(variables_mapping)
self.test_variables_mapping = {}
# priority: extracted variable > teststep variable
self.test_variables_mapping.update(parsed_variables_mapping)
self.test_variables_mapping.update(self.session_variables_mapping)
def __init__(self, variables=None):
variables_mapping = utils.ensure_mapping_format(variables or {})
self.session_variables_mapping = parser.parse_variables_mapping(variables_mapping)
self.test_variables_mapping = {}
self.init_test_variables()
self.__step_datas: List[StepData] = []
self.__session = self.__session or HttpSession()
self.__session_variables = {}
# run teststeps
for step in self.__teststeps:
# override variables
# session variables (extracted from pre step) > step variables
step.variables.update(self.__session_variables)
# step variables > testcase config variables
step.variables = override_config_variables(
step.variables, self.__config.variables
)
# parse variables
step.variables = parse_variables_mapping(
step.variables, self.__project_meta.functions
)
# run step
if USE_ALLURE:
with allure.step(f"step: {step.name}"):
extract_mapping = self.__run_step(step)
else:
extract_mapping = self.__run_step(step)
# save extracted variables to session variables
self.__session_variables.update(extract_mapping)
self.__duration = time.time() - self.__start_at
return self