update: get function or variable item

This commit is contained in:
httprunner
2018-08-09 01:10:03 +08:00
parent 6ba88440e2
commit 72d0fe21e2
3 changed files with 17 additions and 14 deletions

View File

@@ -294,6 +294,15 @@ class TestcaseParser(object):
self.functions = functions self.functions = functions
def _get_bind_item(self, item_type, item_name): def _get_bind_item(self, item_type, item_name):
""" get specified function or variable.
Args:
item_type(str): functions or variables
item_name(str): function name or variable name
Returns:
object: specified function or variable object.
"""
if item_type == "functions": if item_type == "functions":
if item_name in self.functions: if item_name in self.functions:
return self.functions[item_name] return self.functions[item_name]
@@ -307,19 +316,13 @@ class TestcaseParser(object):
except (NameError, TypeError): except (NameError, TypeError):
# is not builtin function, continue to search # is not builtin function, continue to search
pass pass
elif item_type == "variables": else:
# item_type == "variables":
if item_name in self.variables: if item_name in self.variables:
return self.variables[item_name] return self.variables[item_name]
else:
raise exceptions.ParamsError("bind item should only be function or variable.")
try: debugtalk_module = loader.load_debugtalk_module(self.file_path)
assert self.file_path is not None return loader.get_module_item(debugtalk_module, item_type, item_name)
debugtalk_module = loader.load_debugtalk_module(self.file_path)
return loader.get_module_item(debugtalk_module, item_type, item_name)
except (AssertionError, exceptions.FunctionNotFound):
raise exceptions.ParamsError(
"{} is not defined in bind {}s!".format(item_name, item_type))
def get_bind_function(self, func_name): def get_bind_function(self, func_name):
return self._get_bind_item("functions", func_name) return self._get_bind_item("functions", func_name)

View File

@@ -250,7 +250,7 @@ class VariableBindsUnittest(ApiServerUnittest):
variables = [] variables = []
self.context.bind_variables(variables) self.context.bind_variables(variables)
with self.assertRaises(exceptions.ParamsError): with self.assertRaises(exceptions.VariableNotFound):
self.context.validate(validators, resp_obj) self.context.validate(validators, resp_obj)
# expected value missed in variables mapping # expected value missed in variables mapping

View File

@@ -277,7 +277,7 @@ class TestTestcaseParser(unittest.TestCase):
def test_eval_content_variables_search_upward(self): def test_eval_content_variables_search_upward(self):
testcase_parser = parser.TestcaseParser() testcase_parser = parser.TestcaseParser()
with self.assertRaises(exceptions.ParamsError): with self.assertRaises(exceptions.VariableNotFound):
testcase_parser._eval_content_variables("/api/$SECRET_KEY") testcase_parser._eval_content_variables("/api/$SECRET_KEY")
testcase_parser.file_path = "tests/data/demo_testset_hardcode.yml" testcase_parser.file_path = "tests/data/demo_testset_hardcode.yml"
@@ -300,7 +300,7 @@ class TestTestcaseParser(unittest.TestCase):
"123str_value1/456" "123str_value1/456"
) )
with self.assertRaises(exceptions.ParamsError): with self.assertRaises(exceptions.VariableNotFound):
testcase_parser.eval_content_with_bindings("$str_3") testcase_parser.eval_content_with_bindings("$str_3")
self.assertEqual( self.assertEqual(
@@ -410,7 +410,7 @@ class TestTestcaseParser(unittest.TestCase):
def test_eval_content_functions_search_upward(self): def test_eval_content_functions_search_upward(self):
testcase_parser = parser.TestcaseParser() testcase_parser = parser.TestcaseParser()
with self.assertRaises(exceptions.ParamsError): with self.assertRaises(exceptions.FunctionNotFound):
testcase_parser._eval_content_functions("/api/${gen_md5(abc)}") testcase_parser._eval_content_functions("/api/${gen_md5(abc)}")
testcase_parser.file_path = "tests/data/demo_testset_hardcode.yml" testcase_parser.file_path = "tests/data/demo_testset_hardcode.yml"