mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 12:33:59 +08:00
remove debugtalk.py variables mechanism
This commit is contained in:
@@ -13,10 +13,12 @@ class Context(object):
|
||||
def __init__(self, variables=None, functions=None):
|
||||
""" init Context with testcase variables and functions.
|
||||
"""
|
||||
variables = variables or {}
|
||||
functions = functions or {}
|
||||
# testcase level context
|
||||
## TESTCASE_SHARED_VARIABLES_MAPPING and TESTCASE_SHARED_FUNCTIONS_MAPPING are unchangeable.
|
||||
self.TESTCASE_SHARED_VARIABLES_MAPPING = utils.ensure_mapping_format(variables)
|
||||
self.TESTCASE_SHARED_FUNCTIONS_MAPPING = functions or OrderedDict()
|
||||
self.TESTCASE_SHARED_FUNCTIONS_MAPPING = functions
|
||||
|
||||
# testcase level request, will not change
|
||||
self.TESTCASE_SHARED_REQUEST_MAPPING = {}
|
||||
|
||||
@@ -8,7 +8,7 @@ import os
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
from httprunner import built_in, exceptions, logger, parser, utils, validator
|
||||
from httprunner import exceptions, logger, parser, utils, validator
|
||||
from httprunner.compat import OrderedDict
|
||||
|
||||
###############################################################################
|
||||
@@ -179,6 +179,7 @@ def load_dot_env_file(dot_env_path):
|
||||
|
||||
env_variables_mapping[variable.strip()] = value.strip()
|
||||
|
||||
utils.set_os_environ(env_variables_mapping)
|
||||
return env_variables_mapping
|
||||
|
||||
|
||||
@@ -219,96 +220,58 @@ def locate_file(start_path, file_name):
|
||||
## debugtalk.py module loader
|
||||
###############################################################################
|
||||
|
||||
def load_python_module(module):
|
||||
""" load python module.
|
||||
def load_module_functions(module):
|
||||
""" load python module functions.
|
||||
|
||||
Args:
|
||||
module: python module
|
||||
|
||||
Returns:
|
||||
dict: variables and functions mapping for specified python module
|
||||
dict: functions mapping for specified python module
|
||||
|
||||
{
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
"func1_name": func1,
|
||||
"func2_name": func2
|
||||
}
|
||||
|
||||
"""
|
||||
# TODO (2.0): remove variables from debugtalk.py
|
||||
debugtalk_module = {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
}
|
||||
module_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)):
|
||||
if isinstance(item, tuple):
|
||||
continue
|
||||
debugtalk_module["variables"][name] = item
|
||||
else:
|
||||
pass
|
||||
module_functions[name] = item
|
||||
|
||||
return debugtalk_module
|
||||
return module_functions
|
||||
|
||||
|
||||
def load_builtin_module():
|
||||
""" load built_in module
|
||||
def load_builtin_functions():
|
||||
""" load built_in module functions
|
||||
"""
|
||||
built_in_module = load_python_module(built_in)
|
||||
return built_in_module
|
||||
from httprunner import built_in
|
||||
return load_module_functions(built_in)
|
||||
|
||||
|
||||
def load_debugtalk_module():
|
||||
""" load project debugtalk.py module
|
||||
def load_debugtalk_functions(debugtalk_path):
|
||||
""" load project debugtalk.py module functions
|
||||
debugtalk.py should be located in project working directory.
|
||||
|
||||
Args:
|
||||
debugtalk_path(str): debugtalk.py path
|
||||
|
||||
Returns:
|
||||
dict: debugtalk module mapping
|
||||
dict: debugtalk module functions mapping
|
||||
{
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
"func1_name": func1,
|
||||
"func2_name": func2
|
||||
}
|
||||
|
||||
"""
|
||||
if not debugtalk_path:
|
||||
return {}
|
||||
|
||||
# load debugtalk.py module
|
||||
imported_module = importlib.import_module("debugtalk")
|
||||
debugtalk_module = load_python_module(imported_module)
|
||||
return debugtalk_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)
|
||||
return load_module_functions(imported_module)
|
||||
|
||||
|
||||
###############################################################################
|
||||
@@ -899,7 +862,7 @@ def load_project_tests(test_path, dot_env_path=None):
|
||||
dot_env_path (str): specified .env file path
|
||||
|
||||
Returns:
|
||||
dict: project loaded api/testcases definitions, environments and debugtalk.py module.
|
||||
dict: project loaded api/testcases definitions, environments and debugtalk.py functions.
|
||||
|
||||
"""
|
||||
project_mapping = {}
|
||||
@@ -924,13 +887,7 @@ def load_project_tests(test_path, dot_env_path=None):
|
||||
project_mapping["env"] = {}
|
||||
|
||||
# load debugtalk.py
|
||||
if debugtalk_path:
|
||||
project_mapping["debugtalk"] = load_debugtalk_module()
|
||||
else:
|
||||
project_mapping["debugtalk"] = {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
}
|
||||
project_mapping["functions"] = load_debugtalk_functions(debugtalk_path)
|
||||
|
||||
project_mapping["def-api"] = load_api_folder(os.path.join(project_working_directory, "api"))
|
||||
# TODO: replace suite with testcases
|
||||
@@ -960,10 +917,7 @@ def load_tests(path, dot_env_path=None):
|
||||
"variables": [], # optional
|
||||
"request": {} # optional
|
||||
"refs": {
|
||||
"debugtalk": {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
},
|
||||
"functions": {},
|
||||
"env": {},
|
||||
"def-api": {},
|
||||
"def-testcase": {}
|
||||
@@ -1049,10 +1003,7 @@ def load_locust_tests(path, dot_env_path=None):
|
||||
raw_testcase = load_file(path)
|
||||
project_mapping = load_project_tests(path, dot_env_path)
|
||||
|
||||
config = {
|
||||
"variables": project_mapping["debugtalk"]["variables"],
|
||||
"functions": project_mapping["debugtalk"]["functions"]
|
||||
}
|
||||
config = {}
|
||||
tests = []
|
||||
for item in raw_testcase:
|
||||
key, test_block = item.popitem()
|
||||
@@ -1067,31 +1018,25 @@ def load_locust_tests(path, dot_env_path=None):
|
||||
|
||||
# parse config variables
|
||||
raw_config_variables = config.get("variables", [])
|
||||
parsed_config_variables = parser.parse_data(
|
||||
raw_config_variables,
|
||||
project_mapping["debugtalk"]["variables"],
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
)
|
||||
|
||||
# priority: passed in > debugtalk.py > parameters > variables
|
||||
# override variables mapping with parameters mapping
|
||||
config_variables = utils.override_mapping_list(
|
||||
parsed_config_variables, {})
|
||||
# merge debugtalk.py module variables
|
||||
config_variables.update(project_mapping["debugtalk"]["variables"])
|
||||
config_variables = parser.parse_data(
|
||||
raw_config_variables,
|
||||
{},
|
||||
project_mapping["functions"]
|
||||
)
|
||||
|
||||
# parse config name
|
||||
config["name"] = parser.parse_data(
|
||||
config.get("name", ""),
|
||||
config_variables,
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
project_mapping["functions"]
|
||||
)
|
||||
|
||||
# parse config request
|
||||
config["request"] = parser.parse_data(
|
||||
config.get("request", {}),
|
||||
config_variables,
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
project_mapping["functions"]
|
||||
)
|
||||
|
||||
return {
|
||||
|
||||
@@ -325,34 +325,6 @@ def parse_parameters(parameters, variables_mapping, functions_mapping):
|
||||
## parse content with variables and functions mapping
|
||||
###############################################################################
|
||||
|
||||
def get_builtin_item(item_type, item_name):
|
||||
"""
|
||||
|
||||
Args:
|
||||
item_type (enum): "variables" or "functions"
|
||||
item_name (str): variable name or function name
|
||||
|
||||
Returns:
|
||||
variable or function with the name of item_name
|
||||
|
||||
"""
|
||||
# override built_in module with debugtalk.py module
|
||||
from httprunner import loader
|
||||
built_in_module = loader.load_builtin_module()
|
||||
|
||||
if item_type == "variables":
|
||||
try:
|
||||
return built_in_module["variables"][item_name]
|
||||
except KeyError:
|
||||
raise exceptions.VariableNotFound("{} is not found.".format(item_name))
|
||||
else:
|
||||
# item_type == "functions":
|
||||
try:
|
||||
return built_in_module["functions"][item_name]
|
||||
except KeyError:
|
||||
raise exceptions.FunctionNotFound("{} is not found.".format(item_name))
|
||||
|
||||
|
||||
def get_mapping_variable(variable_name, variables_mapping):
|
||||
""" get variable from variables_mapping.
|
||||
|
||||
@@ -367,10 +339,10 @@ def get_mapping_variable(variable_name, variables_mapping):
|
||||
exceptions.VariableNotFound: variable is not found.
|
||||
|
||||
"""
|
||||
if variable_name in variables_mapping:
|
||||
try:
|
||||
return variables_mapping[variable_name]
|
||||
else:
|
||||
return get_builtin_item("variables", variable_name)
|
||||
except KeyError:
|
||||
raise exceptions.VariableNotFound("{} is not found.".format(variable_name))
|
||||
|
||||
|
||||
def get_mapping_function(function_name, functions_mapping):
|
||||
@@ -392,12 +364,15 @@ def get_mapping_function(function_name, functions_mapping):
|
||||
return functions_mapping[function_name]
|
||||
|
||||
try:
|
||||
return get_builtin_item("functions", function_name)
|
||||
except exceptions.FunctionNotFound:
|
||||
# check if HttpRunner builtin functions
|
||||
from httprunner import loader
|
||||
built_in_functions = loader.load_builtin_functions()
|
||||
return built_in_functions[function_name]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
try:
|
||||
# check if builtin functions
|
||||
# check if Python builtin functions
|
||||
item_func = eval(function_name)
|
||||
if callable(item_func):
|
||||
# is builtin function
|
||||
@@ -608,10 +583,7 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
project_mapping = testcase_config.pop(
|
||||
"refs",
|
||||
{
|
||||
"debugtalk": {
|
||||
"variables": {},
|
||||
"functions": {}
|
||||
},
|
||||
"functions": {},
|
||||
"env": {},
|
||||
"def-api": {},
|
||||
"def-testcase": {}
|
||||
@@ -626,8 +598,8 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
config_parameters = testcase_config.pop("parameters", [])
|
||||
cartesian_product_parameters_list = parse_parameters(
|
||||
config_parameters,
|
||||
project_mapping["debugtalk"]["variables"],
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
{},
|
||||
project_mapping["functions"]
|
||||
) or [{}]
|
||||
|
||||
for parameter_mapping in cartesian_product_parameters_list:
|
||||
@@ -637,10 +609,9 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
raw_config_variables = config.get("variables", [])
|
||||
raw_config_variables_mapping = utils.ensure_mapping_format(raw_config_variables)
|
||||
|
||||
# priority: passed in > .env > debugtalk.py > parameters > variables
|
||||
# priority: passed in > parameters > variables
|
||||
|
||||
config_variables = utils.deepcopy_dict(parameter_mapping)
|
||||
config_variables.update(project_mapping["debugtalk"]["variables"])
|
||||
config_variables.update(variables_mapping)
|
||||
|
||||
for key, value in raw_config_variables_mapping.items():
|
||||
@@ -653,7 +624,7 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
parsed_value = parse_data(
|
||||
value,
|
||||
config_variables,
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
project_mapping["functions"]
|
||||
)
|
||||
config_variables[key] = parsed_value
|
||||
|
||||
@@ -663,18 +634,18 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
testcase_dict["config"]["name"] = parse_data(
|
||||
testcase_dict["config"].get("name", ""),
|
||||
config_variables,
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
project_mapping["functions"]
|
||||
)
|
||||
|
||||
# parse config request
|
||||
testcase_dict["config"]["request"] = parse_data(
|
||||
testcase_dict["config"].get("request", {}),
|
||||
config_variables,
|
||||
project_mapping["debugtalk"]["functions"]
|
||||
project_mapping["functions"]
|
||||
)
|
||||
|
||||
# put loaded project functions to config
|
||||
testcase_dict["config"]["functions"] = project_mapping["debugtalk"]["functions"]
|
||||
testcase_dict["config"]["functions"] = project_mapping["functions"]
|
||||
parsed_testcases_list.append(testcase_dict)
|
||||
|
||||
# unset OS environment variables
|
||||
|
||||
Reference in New Issue
Block a user