mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 00:09:37 +08:00
parse_parameters: add csv support
This commit is contained in:
@@ -5,72 +5,6 @@ import copy
|
|||||||
from httprunner import exceptions, logger, parser, utils
|
from httprunner import exceptions, logger, parser, utils
|
||||||
from httprunner.compat import OrderedDict
|
from httprunner.compat import OrderedDict
|
||||||
|
|
||||||
# def parse_parameters(parameters, testset_path=None):
|
|
||||||
# """ parse parameters and generate cartesian product.
|
|
||||||
|
|
||||||
# Args:
|
|
||||||
# parameters (list) parameters: parameter name and value in list
|
|
||||||
# parameter value may be in three types:
|
|
||||||
# (1) data list, e.g. ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
|
|
||||||
# (2) call built-in parameterize function, "${parameterize(account.csv)}"
|
|
||||||
# (3) call custom function in debugtalk.py, "${gen_app_version()}"
|
|
||||||
|
|
||||||
# testset_path (str): testset file path, used for locating csv file and debugtalk.py
|
|
||||||
|
|
||||||
# Returns:
|
|
||||||
# list: cartesian product list
|
|
||||||
|
|
||||||
# Examples:
|
|
||||||
# >>> parameters = [
|
|
||||||
# {"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]},
|
|
||||||
# {"username-password": "${parameterize(account.csv)}"},
|
|
||||||
# {"app_version": "${gen_app_version()}"}
|
|
||||||
# ]
|
|
||||||
# >>> parse_parameters(parameters)
|
|
||||||
|
|
||||||
# """
|
|
||||||
# testcase_parser = TestcaseParser(file_path=testset_path)
|
|
||||||
|
|
||||||
# parsed_parameters_list = []
|
|
||||||
# for parameter in parameters:
|
|
||||||
# parameter_name, parameter_content = list(parameter.items())[0]
|
|
||||||
# parameter_name_list = parameter_name.split("-")
|
|
||||||
|
|
||||||
# if isinstance(parameter_content, list):
|
|
||||||
# # (1) data list
|
|
||||||
# # e.g. {"app_version": ["2.8.5", "2.8.6"]}
|
|
||||||
# # => [{"app_version": "2.8.5", "app_version": "2.8.6"}]
|
|
||||||
# # e.g. {"username-password": [["user1", "111111"], ["test2", "222222"]}
|
|
||||||
# # => [{"username": "user1", "password": "111111"}, {"username": "user2", "password": "222222"}]
|
|
||||||
# parameter_content_list = []
|
|
||||||
# for parameter_item in parameter_content:
|
|
||||||
# if not isinstance(parameter_item, (list, tuple)):
|
|
||||||
# # "2.8.5" => ["2.8.5"]
|
|
||||||
# parameter_item = [parameter_item]
|
|
||||||
|
|
||||||
# # ["app_version"], ["2.8.5"] => {"app_version": "2.8.5"}
|
|
||||||
# # ["username", "password"], ["user1", "111111"] => {"username": "user1", "password": "111111"}
|
|
||||||
# parameter_content_dict = dict(zip(parameter_name_list, parameter_item))
|
|
||||||
|
|
||||||
# parameter_content_list.append(parameter_content_dict)
|
|
||||||
# else:
|
|
||||||
# # (2) & (3)
|
|
||||||
# parsed_parameter_content = testcase_parser.eval_content_with_bindings(parameter_content)
|
|
||||||
# # e.g. [{'app_version': '2.8.5'}, {'app_version': '2.8.6'}]
|
|
||||||
# # e.g. [{"username": "user1", "password": "111111"}, {"username": "user2", "password": "222222"}]
|
|
||||||
# if not isinstance(parsed_parameter_content, list):
|
|
||||||
# raise exceptions.ParamsError("parameters syntax error!")
|
|
||||||
|
|
||||||
# parameter_content_list = [
|
|
||||||
# # get subset by parameter name
|
|
||||||
# {key: parameter_item[key] for key in parameter_name_list}
|
|
||||||
# for parameter_item in parsed_parameter_content
|
|
||||||
# ]
|
|
||||||
|
|
||||||
# parsed_parameters_list.append(parameter_content_list)
|
|
||||||
|
|
||||||
# return utils.gen_cartesian_product(*parsed_parameters_list)
|
|
||||||
|
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
""" Manages context functions and variables.
|
""" Manages context functions and variables.
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ from httprunner import built_in, exceptions, logger, parser, validator
|
|||||||
from httprunner.compat import OrderedDict
|
from httprunner.compat import OrderedDict
|
||||||
|
|
||||||
project_mapping = {
|
project_mapping = {
|
||||||
"debugtalk": {},
|
"debugtalk": {
|
||||||
|
"variables": {},
|
||||||
|
"functions": {}
|
||||||
|
},
|
||||||
"env": {},
|
"env": {},
|
||||||
"def-api": {},
|
"def-api": {},
|
||||||
"def-testcase": {}
|
"def-testcase": {}
|
||||||
@@ -887,7 +890,10 @@ def load_test_folder(test_folder_path=None):
|
|||||||
def reset_loader():
|
def reset_loader():
|
||||||
""" reset project mapping.
|
""" reset project mapping.
|
||||||
"""
|
"""
|
||||||
project_mapping["debugtalk"] = {}
|
project_mapping["debugtalk"] = {
|
||||||
|
"variables": {},
|
||||||
|
"functions": {}
|
||||||
|
}
|
||||||
project_mapping["env"] = {}
|
project_mapping["env"] = {}
|
||||||
project_mapping["def-api"] = {}
|
project_mapping["def-api"] = {}
|
||||||
project_mapping["def-testcase"] = {}
|
project_mapping["def-testcase"] = {}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ from httprunner import exceptions, utils
|
|||||||
from httprunner.compat import basestring, builtin_str, numeric_types, str
|
from httprunner.compat import basestring, builtin_str, numeric_types, str
|
||||||
|
|
||||||
variable_regexp = r"\$([\w_]+)"
|
variable_regexp = r"\$([\w_]+)"
|
||||||
function_regexp = r"\$\{([\w_]+\([\$\w\.\-_ =,]*\))\}"
|
function_regexp = r"\$\{([\w_]+\([\$\w\.\-/_ =,]*\))\}"
|
||||||
function_regexp_compile = re.compile(r"^([\w_]+)\(([\$\w\.\-_ =,]*)\)$")
|
function_regexp_compile = re.compile(r"^([\w_]+)\(([\$\w\.\-/_ =,]*)\)$")
|
||||||
|
|
||||||
|
|
||||||
def parse_string_value(str_value):
|
def parse_string_value(str_value):
|
||||||
@@ -255,7 +255,6 @@ def substitute_variables(content, variables_mapping):
|
|||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
def parse_parameters(parameters, variables_mapping, functions_mapping):
|
def parse_parameters(parameters, variables_mapping, functions_mapping):
|
||||||
""" parse parameters and generate cartesian product.
|
""" parse parameters and generate cartesian product.
|
||||||
|
|
||||||
@@ -404,9 +403,8 @@ def parse_string_functions(content, variables_mapping, functions_mapping):
|
|||||||
kwargs = parse_data(kwargs, variables_mapping, functions_mapping)
|
kwargs = parse_data(kwargs, variables_mapping, functions_mapping)
|
||||||
|
|
||||||
if func_name in ["parameterize", "P"]:
|
if func_name in ["parameterize", "P"]:
|
||||||
# TODO: add parameterize
|
from httprunner import loader
|
||||||
# eval_value = load_csv_list(*args, **kwargs)
|
eval_value = loader.load_csv_file(*args, **kwargs)
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
func = get_mapping_function(func_name, functions_mapping)
|
func = get_mapping_function(func_name, functions_mapping)
|
||||||
eval_value = func(*args, **kwargs)
|
eval_value = func(*args, **kwargs)
|
||||||
|
|||||||
@@ -384,8 +384,6 @@ class TestParser(unittest.TestCase):
|
|||||||
os.getcwd(),
|
os.getcwd(),
|
||||||
"tests/data/demo_parameters.yml"
|
"tests/data/demo_parameters.yml"
|
||||||
)
|
)
|
||||||
variables_mapping = {}
|
|
||||||
functions_mapping = {}
|
|
||||||
from tests import debugtalk
|
from tests import debugtalk
|
||||||
debugtalk_module = loader.load_python_module(debugtalk)
|
debugtalk_module = loader.load_python_module(debugtalk)
|
||||||
cartesian_product_parameters = parser.parse_parameters(
|
cartesian_product_parameters = parser.parse_parameters(
|
||||||
@@ -398,32 +396,40 @@ class TestParser(unittest.TestCase):
|
|||||||
2 * 2
|
2 * 2
|
||||||
)
|
)
|
||||||
|
|
||||||
# def test_parse_parameters_parameterize(self):
|
def test_parse_parameters_parameterize(self):
|
||||||
# parameters = [
|
parameters = [
|
||||||
# {"app_version": "${parameterize(app_version.csv)}"},
|
{"app_version": "${parameterize(tests/data/app_version.csv)}"},
|
||||||
# {"username-password": "${parameterize(account.csv)}"}
|
{"username-password": "${parameterize(tests/data/account.csv)}"}
|
||||||
# ]
|
]
|
||||||
|
variables_mapping = {}
|
||||||
|
functions_mapping = {}
|
||||||
|
|
||||||
# cartesian_product_parameters = parser.parse_parameters(
|
cartesian_product_parameters = parser.parse_parameters(
|
||||||
# parameters, variables_mapping, functions_mapping)
|
parameters, variables_mapping, functions_mapping)
|
||||||
# self.assertEqual(
|
self.assertEqual(
|
||||||
# len(cartesian_product_parameters),
|
len(cartesian_product_parameters),
|
||||||
# 2 * 3
|
2 * 3
|
||||||
# )
|
)
|
||||||
|
|
||||||
# def test_parse_parameters_mix(self):
|
def test_parse_parameters_mix(self):
|
||||||
# parameters = [
|
project_dir = os.path.join(os.getcwd(), "tests")
|
||||||
# {"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]},
|
loader.load_debugtalk_module(project_dir)
|
||||||
# {"app_version": "${gen_app_version()}"},
|
project_mapping = loader.project_mapping
|
||||||
# {"username-password": "${parameterize(account.csv)}"}
|
|
||||||
# ]
|
parameters = [
|
||||||
# testset_path = os.path.join(
|
{"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]},
|
||||||
# os.getcwd(),
|
{"app_version": "${gen_app_version()}"},
|
||||||
# "tests/data/demo_parameters.yml"
|
{"username-password": "${parameterize(tests/data/account.csv)}"}
|
||||||
# )
|
]
|
||||||
# cartesian_product_parameters = parser.parse_parameters(
|
variables_mapping = {}
|
||||||
# parameters, variables_mapping, functions_mapping)
|
functions_mapping = project_mapping["debugtalk"]["functions"]
|
||||||
# self.assertEqual(
|
testset_path = os.path.join(
|
||||||
# len(cartesian_product_parameters),
|
os.getcwd(),
|
||||||
# 3 * 2 * 3
|
"tests/data/demo_parameters.yml"
|
||||||
# )
|
)
|
||||||
|
cartesian_product_parameters = parser.parse_parameters(
|
||||||
|
parameters, variables_mapping, functions_mapping)
|
||||||
|
self.assertEqual(
|
||||||
|
len(cartesian_product_parameters),
|
||||||
|
3 * 2 * 3
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user