mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
replace filter_module with load_python_module
This commit is contained in:
@@ -5,7 +5,7 @@ import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from httprunner import built_in, exceptions, logger, parser, utils
|
||||
from httprunner import built_in, exceptions, loader, logger, parser, utils
|
||||
from httprunner.compat import OrderedDict
|
||||
|
||||
|
||||
@@ -69,11 +69,9 @@ class Context(object):
|
||||
def import_module_items(self, imported_module):
|
||||
""" import module functions and variables and bind to testset context
|
||||
"""
|
||||
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, "testset")
|
||||
module_mapping = loader.load_python_module(imported_module)
|
||||
self.__update_context_functions_config("testset", module_mapping["functions"])
|
||||
self.bind_variables(module_mapping["variables"], "testset")
|
||||
|
||||
def bind_variables(self, variables, level="testcase"):
|
||||
""" bind variables to testset context or current testcase context.
|
||||
|
||||
@@ -207,6 +207,37 @@ def locate_debugtalk_py(start_path):
|
||||
return locate_debugtalk_py(os.path.dirname(start_dir_path))
|
||||
|
||||
|
||||
def load_python_module(module):
|
||||
""" load python module.
|
||||
|
||||
Args:
|
||||
module: python module
|
||||
|
||||
Returns:
|
||||
dict: variables and functions mapping for specified python module
|
||||
|
||||
{
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
}
|
||||
|
||||
"""
|
||||
debugtalk_module = {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
}
|
||||
|
||||
for name, item in vars(module).items():
|
||||
if validator.is_function((name, item)):
|
||||
debugtalk_module["functions"][name] = item
|
||||
elif validator.is_variable((name, item)):
|
||||
debugtalk_module["variables"][name] = item
|
||||
else:
|
||||
pass
|
||||
|
||||
return debugtalk_module
|
||||
|
||||
|
||||
def load_debugtalk_module(start_path=None):
|
||||
""" load debugtalk.py module.
|
||||
|
||||
@@ -224,26 +255,17 @@ def load_debugtalk_module(start_path=None):
|
||||
|
||||
"""
|
||||
start_path = start_path or os.getcwd()
|
||||
debugtalk_module = {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
}
|
||||
|
||||
try:
|
||||
module_name = locate_debugtalk_py(start_path)
|
||||
except exceptions.FileNotFound:
|
||||
return debugtalk_module
|
||||
return {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
}
|
||||
|
||||
imported_module = importlib.import_module(module_name)
|
||||
for name, item in vars(imported_module).items():
|
||||
if validator.is_function((name, item)):
|
||||
debugtalk_module["functions"][name] = item
|
||||
elif validator.is_variable((name, item)):
|
||||
debugtalk_module["variables"][name] = item
|
||||
else:
|
||||
pass
|
||||
|
||||
return debugtalk_module
|
||||
return load_python_module(imported_module)
|
||||
|
||||
|
||||
###############################################################################
|
||||
|
||||
@@ -172,6 +172,12 @@ class TestModuleLoader(unittest.TestCase):
|
||||
"tests.debugtalk"
|
||||
)
|
||||
|
||||
def test_filter_module_functions(self):
|
||||
module_mapping = loader.load_python_module(loader)
|
||||
functions_dict = module_mapping["functions"]
|
||||
self.assertIn("load_python_module", functions_dict)
|
||||
self.assertNotIn("is_py3", functions_dict)
|
||||
|
||||
def test_load_debugtalk_module(self):
|
||||
imported_module_items = loader.load_debugtalk_module()
|
||||
self.assertEqual(imported_module_items["functions"], {})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import requests
|
||||
from httprunner import built_in, exceptions, response, utils
|
||||
from httprunner import built_in, exceptions, loader, response
|
||||
from httprunner.compat import basestring, bytes
|
||||
from tests.base import HTTPBIN_SERVER, ApiServerUnittest
|
||||
|
||||
@@ -7,7 +7,8 @@ from tests.base import HTTPBIN_SERVER, ApiServerUnittest
|
||||
class TestResponse(ApiServerUnittest):
|
||||
|
||||
def setUp(self):
|
||||
self.functions_mapping = utils.filter_module(built_in, "function")
|
||||
module_mapping = loader.load_python_module(built_in)
|
||||
self.functions_mapping = module_mapping["functions"]
|
||||
|
||||
def test_parse_response_object_json(self):
|
||||
url = "http://127.0.0.1:5000/api/users"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from httprunner import exceptions, utils, validator
|
||||
from httprunner import exceptions, loader, utils, validator
|
||||
from httprunner.compat import OrderedDict
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
@@ -100,7 +100,8 @@ class TestUtils(ApiServerUnittest):
|
||||
|
||||
def current_validators(self):
|
||||
from httprunner import built_in
|
||||
functions_mapping = utils.filter_module(built_in, "function")
|
||||
module_mapping = loader.load_python_module(built_in)
|
||||
functions_mapping = module_mapping["functions"]
|
||||
|
||||
functions_mapping["equals"](None, None)
|
||||
functions_mapping["equals"](1, 1)
|
||||
@@ -154,16 +155,12 @@ class TestUtils(ApiServerUnittest):
|
||||
{'a': 2, 'b': {'c': 33, 'd': 4, 'e': 5}, 'f': 6, 'g': 7, 'h': 123}
|
||||
)
|
||||
|
||||
def test_filter_module_functions(self):
|
||||
functions_dict = utils.filter_module(utils, "function")
|
||||
self.assertIn("filter_module", functions_dict)
|
||||
self.assertNotIn("is_py3", functions_dict)
|
||||
|
||||
def test_get_imported_module_from_file(self):
|
||||
imported_module = utils.get_imported_module_from_file("tests/debugtalk.py")
|
||||
self.assertIn("gen_md5", dir(imported_module))
|
||||
|
||||
functions_dict = utils.filter_module(imported_module, "function")
|
||||
module_mapping = loader.load_python_module(imported_module)
|
||||
functions_dict = module_mapping["functions"]
|
||||
self.assertIn("gen_md5", functions_dict)
|
||||
self.assertNotIn("urllib", functions_dict)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user