def test_eval_content_variables_search_upward(self): testcase_parser = context.TestcaseParser() with self.assertRaises(exceptions.VariableNotFound): testcase_parser._eval_content_variables("/api/$SECRET_KEY") testcase_parser.file_path = "tests/data/demo_testset_hardcode.yml" content = testcase_parser._eval_content_variables("/api/$SECRET_KEY") self.assertEqual(content, "/api/DebugTalk")
def test_eval_content_functions(self): functions = {"add_two_nums": lambda a, b=1: a + b} testcase_parser = context.TestcaseParser(functions=functions) self.assertEqual( testcase_parser._eval_content_functions("${add_two_nums(1, 2)}"), 3) self.assertEqual( testcase_parser._eval_content_functions( "/api/${add_two_nums(1, 2)}"), "/api/3")
def test_eval_content_functions_search_upward(self): testcase_parser = context.TestcaseParser() with self.assertRaises(exceptions.FunctionNotFound): testcase_parser._eval_content_functions("/api/${gen_md5(abc)}") testcase_parser.file_path = "tests/data/demo_testset_hardcode.yml" content = testcase_parser._eval_content_functions( "/api/${gen_md5(abc)}") self.assertEqual(content, "/api/900150983cd24fb0d6963f7d28e17f72")
def __init__(self, testset, variables_mapping=None, http_client_session=None): super(TestSuite, self).__init__() self.test_runner_list = [] self.config = testset.get("config", {}) self.output_variables_list = self.config.get("output", []) self.testset_file_path = self.config.get("path") config_dict_parameters = self.config.get("parameters", []) config_dict_variables = self.config.get("variables", []) variables_mapping = variables_mapping or {} config_dict_variables = utils.override_variables_binds( config_dict_variables, variables_mapping) config_parametered_variables_list = self._get_parametered_variables( config_dict_variables, config_dict_parameters) self.testcase_parser = context.TestcaseParser() testcases = testset.get("testcases", []) for config_variables in config_parametered_variables_list: # config level self.config["variables"] = config_variables test_runner = runner.Runner(self.config, http_client_session) for testcase_dict in testcases: testcase_dict = copy.copy(testcase_dict) # testcase level testcase_parametered_variables_list = self._get_parametered_variables( testcase_dict.get("variables", []), testcase_dict.get("parameters", [])) for testcase_variables in testcase_parametered_variables_list: testcase_dict["variables"] = testcase_variables # eval testcase name with bind variables variables = utils.override_variables_binds( config_variables, testcase_variables) self.testcase_parser.update_binded_variables(variables) try: testcase_name = self.testcase_parser.eval_content_with_bindings( testcase_dict["name"]) except (AssertionError, exceptions.ParamsError): logger.log_warning( "failed to eval testcase name: {}".format( testcase_dict["name"])) testcase_name = testcase_dict["name"] self.test_runner_list.append((test_runner, variables)) self._add_test_to_suite(testcase_name, test_runner, testcase_dict)
def test_parse_content_with_bindings_variables(self): variables = {"str_1": "str_value1", "str_2": "str_value2"} testcase_parser = context.TestcaseParser(variables=variables) self.assertEqual(testcase_parser.eval_content_with_bindings("$str_1"), "str_value1") self.assertEqual( testcase_parser.eval_content_with_bindings("123$str_1/456"), "123str_value1/456") with self.assertRaises(exceptions.VariableNotFound): testcase_parser.eval_content_with_bindings("$str_3") self.assertEqual( testcase_parser.eval_content_with_bindings(["$str_1", "str3"]), ["str_value1", "str3"]) self.assertEqual( testcase_parser.eval_content_with_bindings({"key": "$str_1"}), {"key": "str_value1"})
def test_parse_content_with_bindings_functions(self): import random, string functions = { "gen_random_string": lambda str_len: ''.join(random.choice(string.ascii_letters + string.digits) \ for _ in range(str_len)) } testcase_parser = context.TestcaseParser(functions=functions) result = testcase_parser.eval_content_with_bindings( "${gen_random_string(5)}") self.assertEqual(len(result), 5) add_two_nums = lambda a, b=1: a + b functions["add_two_nums"] = add_two_nums self.assertEqual( testcase_parser.eval_content_with_bindings("${add_two_nums(1)}"), 2) self.assertEqual( testcase_parser.eval_content_with_bindings( "${add_two_nums(1, 2)}"), 3)
def test_eval_content_variables(self): variables = { "var_1": "abc", "var_2": "def", "var_3": 123, "var_4": { "a": 1 }, "var_5": True, "var_6": None } testcase_parser = context.TestcaseParser(variables=variables) self.assertEqual(testcase_parser._eval_content_variables("$var_1"), "abc") self.assertEqual(testcase_parser._eval_content_variables("var_1"), "var_1") self.assertEqual(testcase_parser._eval_content_variables("$var_1#XYZ"), "abc#XYZ") self.assertEqual( testcase_parser._eval_content_variables("/$var_1/$var_2/var3"), "/abc/def/var3") self.assertEqual( testcase_parser._eval_content_variables("/$var_1/$var_2/$var_1"), "/abc/def/abc") self.assertEqual( testcase_parser._eval_content_variables( "${func($var_1, $var_2, xyz)}"), "${func(abc, def, xyz)}") self.assertEqual(testcase_parser._eval_content_variables("$var_3"), 123) self.assertEqual(testcase_parser._eval_content_variables("$var_4"), {"a": 1}) self.assertEqual(testcase_parser._eval_content_variables("$var_5"), True) self.assertEqual(testcase_parser._eval_content_variables("abc$var_5"), "abcTrue") self.assertEqual(testcase_parser._eval_content_variables("abc$var_4"), "abc{'a': 1}") self.assertEqual(testcase_parser._eval_content_variables("$var_6"), None)
def test_parse_content_with_bindings_testcase(self): variables = { "uid": "1000", "random": "A2dEx", "authorization": "a83de0ff8d2e896dbd8efb81ba14e17d", "data": { "name": "user", "password": "******" } } functions = { "add_two_nums": lambda a, b=1: a + b, "get_timestamp": lambda: int(time.time() * 1000) } testcase_template = { "url": "http://127.0.0.1:5000/api/users/$uid/${add_two_nums(1,2)}", "method": "POST", "headers": { "Content-Type": "application/json", "authorization": "$authorization", "random": "$random", "sum": "${add_two_nums(1, 2)}" }, "body": "$data" } parsed_testcase = context.TestcaseParser(variables, functions)\ .eval_content_with_bindings(testcase_template) self.assertEqual(parsed_testcase["url"], "http://127.0.0.1:5000/api/users/1000/3") self.assertEqual(parsed_testcase["headers"]["authorization"], variables["authorization"]) self.assertEqual(parsed_testcase["headers"]["random"], variables["random"]) self.assertEqual(parsed_testcase["body"], variables["data"]) self.assertEqual(parsed_testcase["headers"]["sum"], 3)
def test_parse_variables_multiple_identical_variables(self): variables = {"user": 100, "userid": 1000, "data": 1498} testcase_parser = context.TestcaseParser(variables=variables) content = "/users/$user/$userid/$data?userId=$userid&data=$data" self.assertEqual(testcase_parser.eval_content_with_bindings(content), "/users/100/1000/1498?userId=1000&data=1498")