From cc922c8619ae95b904f98ec3e3db7b660bedd81b Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 9 Aug 2018 00:49:45 +0800 Subject: [PATCH] remove search_conf_item --- httprunner/loader.py | 33 +++++++++++++++++++++++++++++++++ httprunner/parser.py | 11 ++++++----- httprunner/utils.py | 40 +--------------------------------------- tests/test_loader.py | 25 ++++++++++++++++++++++++- tests/test_utils.py | 30 ------------------------------ 5 files changed, 64 insertions(+), 75 deletions(-) diff --git a/httprunner/loader.py b/httprunner/loader.py index ca4a6156..124df5cc 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -268,6 +268,39 @@ def load_debugtalk_module(start_path=None): return load_python_module(imported_module) +def get_module_item(module_mapping, item_type, item_name): + """ get expected function or variable from module mapping. + + Args: + module_mapping(dict): module mapping with variables and functions. + + { + "variables": {}, + "functions": {} + } + + item_type(str): "functions" or "variables" + item_name(str): function name or variable name + + Returns: + object: specified variable or function object. + + Raises: + exceptions.FunctionNotFound: If specified function not found in module mapping + exceptions.VariableNotFound: If specified variable not found in module mapping + + """ + try: + return module_mapping[item_type][item_name] + except KeyError: + err_msg = "{} not found in debugtalk.py module!\n".format(item_name) + err_msg += "module mapping: {}".format(module_mapping) + if item_type == "functions": + raise exceptions.FunctionNotFound(err_msg) + else: + raise exceptions.VariableNotFound(err_msg) + + ############################################################################### ## suite loader ############################################################################### diff --git a/httprunner/parser.py b/httprunner/parser.py index 0e68123e..4dc4ee47 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -294,7 +294,7 @@ class TestcaseParser(object): self.functions = functions def _get_bind_item(self, item_type, item_name): - if item_type == "function": + if item_type == "functions": if item_name in self.functions: return self.functions[item_name] @@ -307,7 +307,7 @@ class TestcaseParser(object): except (NameError, TypeError): # is not builtin function, continue to search pass - elif item_type == "variable": + elif item_type == "variables": if item_name in self.variables: return self.variables[item_name] else: @@ -315,16 +315,17 @@ class TestcaseParser(object): try: assert self.file_path is not None - return utils.search_conf_item(self.file_path, 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): - return self._get_bind_item("function", func_name) + return self._get_bind_item("functions", func_name) def get_bind_variable(self, variable_name): - return self._get_bind_item("variable", variable_name) + return self._get_bind_item("variables", variable_name) def parameterize(self, csv_file_name, fetch_method="Sequential"): parameter_file_path = os.path.join( diff --git a/httprunner/utils.py b/httprunner/utils.py index 3c3756bb..ab58f65d 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -148,7 +148,7 @@ def deep_update_dict(origin_dict, override_dict): return origin_dict def get_imported_module_from_file(file_path): - """ import module from python file path and return imported module + """ DEPRECATED: import module from python file path and return imported module """ if is_py3: imported_module = importlib.machinery.SourceFileLoader( @@ -160,44 +160,6 @@ def get_imported_module_from_file(file_path): return imported_module -def filter_module(module, filter_type): - """ filter functions or variables from import module - @params - module: imported module - filter_type: "function" or "variable" - """ - filter_type = validator.is_function if filter_type == "function" else validator.is_variable - module_functions_dict = dict(filter(filter_type, vars(module).items())) - return module_functions_dict - -def search_conf_item(start_path, item_type, item_name): - """ search expected function or variable recursive upward - @param - start_path: search start path - item_type: "function" or "variable" - item_name: function name or variable name - """ - dir_path = os.path.dirname(os.path.abspath(start_path)) - target_file = os.path.join(dir_path, "debugtalk.py") - - if os.path.isfile(target_file): - imported_module = get_imported_module_from_file(target_file) - items_dict = filter_module(imported_module, item_type) - if item_name in items_dict: - return items_dict[item_name] - else: - return search_conf_item(dir_path, item_type, item_name) - - if dir_path == start_path: - # system root path - err_msg = "{} not found in recursive upward path!".format(item_name) - if item_type == "function": - raise exceptions.FunctionNotFound(err_msg) - else: - raise exceptions.VariableNotFound(err_msg) - - return search_conf_item(dir_path, item_type, item_name) - def lower_dict_keys(origin_dict): """ convert keys in dict to lower case e.g. diff --git a/tests/test_loader.py b/tests/test_loader.py index 79f5082f..5069dc80 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -1,7 +1,7 @@ import os import unittest -from httprunner import exceptions, loader +from httprunner import exceptions, loader, validator class TestFileLoader(unittest.TestCase): @@ -194,6 +194,29 @@ class TestModuleLoader(unittest.TestCase): self.assertTrue(is_status_code_200(200)) self.assertFalse(is_status_code_200(500)) + def test_get_module_item_functions(self): + from httprunner import utils + module_mapping = loader.load_python_module(utils) + + gen_md5 = loader.get_module_item(module_mapping, "functions", "gen_md5") + self.assertTrue(validator.is_function(("gen_md5", gen_md5))) + self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72") + + with self.assertRaises(exceptions.FunctionNotFound): + loader.get_module_item(module_mapping, "functions", "gen_md4") + + def test_get_module_item_variables(self): + from httprunner import utils + module_mapping = loader.load_python_module(utils) + + + SECRET_KEY = loader.get_module_item(module_mapping, "variables", "SECRET_KEY") + self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY))) + self.assertEqual(SECRET_KEY, "DebugTalk") + + with self.assertRaises(exceptions.VariableNotFound): + loader.get_module_item(module_mapping, "variables", "SECRET_KEY2") + class TestSuiteLoader(unittest.TestCase): diff --git a/tests/test_utils.py b/tests/test_utils.py index c94dd3f4..03d89bc3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -167,36 +167,6 @@ class TestUtils(ApiServerUnittest): with self.assertRaises(exceptions.FileNotFoundError): utils.get_imported_module_from_file("tests/debugtalk2.py") - def test_search_conf_function(self): - gen_md5 = utils.search_conf_item("tests/data/demo_binds.yml", "function", "gen_md5") - self.assertTrue(validator.is_function(("gen_md5", gen_md5))) - self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72") - - gen_md5 = utils.search_conf_item("tests/data/subfolder/test.yml", "function", "gen_md5") - self.assertTrue(validator.is_function(("_", gen_md5))) - self.assertEqual(gen_md5("abc"), "900150983cd24fb0d6963f7d28e17f72") - - with self.assertRaises(exceptions.FunctionNotFound): - utils.search_conf_item("tests/data/subfolder/test.yml", "function", "func_not_exist") - - with self.assertRaises(exceptions.FunctionNotFound): - utils.search_conf_item("/user/local/bin", "function", "gen_md5") - - def test_search_conf_variable(self): - SECRET_KEY = utils.search_conf_item("tests/data/demo_binds.yml", "variable", "SECRET_KEY") - self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY))) - self.assertEqual(SECRET_KEY, "DebugTalk") - - SECRET_KEY = utils.search_conf_item("tests/data/subfolder/test.yml", "variable", "SECRET_KEY") - self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY))) - self.assertEqual(SECRET_KEY, "DebugTalk") - - with self.assertRaises(exceptions.VariableNotFound): - utils.search_conf_item("tests/data/subfolder/test.yml", "variable", "variable_not_exist") - - with self.assertRaises(exceptions.VariableNotFound): - utils.search_conf_item("/user/local/bin", "variable", "SECRET_KEY") - def test_handle_config_key_case(self): origin_dict = { "Name": "test",