diff --git a/httprunner/context.py b/httprunner/context.py index c8fabf67..b4be7af5 100644 --- a/httprunner/context.py +++ b/httprunner/context.py @@ -5,7 +5,7 @@ import os import re import sys -from httprunner import exceptions, logger, testcase, utils +from httprunner import built_in, exceptions, logger, testcase, utils from httprunner.compat import OrderedDict @@ -39,7 +39,7 @@ class Context(object): self.testcase_parser.update_binded_variables(self.testcase_variables_mapping) if level == "testset": - self.import_module_items(["httprunner.built_in"], "testset") + self.import_module_items(built_in) def config_context(self, config_dict, level): if level == "testset": @@ -66,17 +66,14 @@ class Context(object): self.__update_context_functions_config(level, eval_function_binds) - def import_module_items(self, modules, level="testcase"): - """ import modules and bind all functions within the context + def import_module_items(self, imported_module): + """ import module functions and variables and bind to testset context """ - sys.path.insert(0, os.getcwd()) - for module_name in modules: - imported_module = utils.get_imported_module(module_name) - imported_functions_dict = utils.filter_module(imported_module, "function") - self.__update_context_functions_config(level, imported_functions_dict) + imported_functions_dict = utils.filter_module(imported_module, "function") + self.__update_context_functions_config("testset", imported_functions_dict) - imported_variables_dict = utils.filter_module(imported_module, "variable") - self.bind_variables(imported_variables_dict, level) + imported_variables_dict = utils.filter_module(imported_module, "variable") + self.bind_variables(imported_variables_dict, "testset") def bind_variables(self, variables, level="testcase"): """ bind variables to testset context or current testcase context. diff --git a/tests/test_context.py b/tests/test_context.py index 3c7383a2..7e47d596 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -113,15 +113,14 @@ class VariableBindsUnittest(ApiServerUnittest): self.assertEqual(context_variables["largest"], 8) def test_import_module_items(self): - module_items = ["tests.debugtalk"] variables = [ {"TOKEN": "debugtalk"}, {"random": "${gen_random_string(5)}"}, {"data": '{"name": "user", "password": "123456"}'}, {"authorization": "${gen_md5($TOKEN, $data, $random)}"} ] - - self.context.import_module_items(module_items) + from tests import debugtalk + self.context.import_module_items(debugtalk) self.context.bind_variables(variables) context_variables = self.context.testcase_variables_mapping @@ -163,7 +162,8 @@ class VariableBindsUnittest(ApiServerUnittest): "data": "$data" } } - self.context.import_module_items(["tests.debugtalk"]) + from tests import debugtalk + self.context.import_module_items(debugtalk) self.context.bind_variables(testcase["variables"]) parsed_request = self.context.get_parsed_request(testcase["request"]) self.assertIn("authorization", parsed_request["headers"])