mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 16:07:45 +08:00
test: #931 add test for parameters
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
config:
|
config:
|
||||||
name: "request methods testcase: validate with functions"
|
name: "request methods testcase: validate with parameters"
|
||||||
parameters:
|
parameters:
|
||||||
user_agent: ["iOS/10.1", "iOS/10.2"]
|
user_agent: ["iOS/10.1", "iOS/10.2"]
|
||||||
username-password: ${parameterize(request_methods/account.csv)}
|
username-password: ${parameterize(request_methods/account.csv)}
|
||||||
app_version:
|
app_version: ${get_app_version()}
|
||||||
- ${get_httprunner_version()}
|
|
||||||
variables:
|
variables:
|
||||||
foo1: f1
|
app_version: f1
|
||||||
base_url: "https://postman-echo.com"
|
base_url: "https://postman-echo.com"
|
||||||
verify: False
|
verify: False
|
||||||
|
|
||||||
@@ -16,7 +15,7 @@ teststeps:
|
|||||||
variables:
|
variables:
|
||||||
foo1: $username
|
foo1: $username
|
||||||
foo2: $password
|
foo2: $password
|
||||||
sum_v: "${sum_two(1, 2)}"
|
sum_v: "${sum_two(1, $app_version)}"
|
||||||
request:
|
request:
|
||||||
method: GET
|
method: GET
|
||||||
url: /get
|
url: /get
|
||||||
@@ -30,5 +29,5 @@ teststeps:
|
|||||||
session_foo2: "body.args.foo2"
|
session_foo2: "body.args.foo2"
|
||||||
validate:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- eq: ["body.args.sum_v", "3"]
|
- str_eq: ["body.args.sum_v", "${sum_two(1, $app_version)}"]
|
||||||
# - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] FIXME: TypeError: '<' not supported between instances of 'str' and 'int'
|
# - less_than: ["body.args.sum_v", "${sum_two(2, 2)}"] FIXME: TypeError: '<' not supported between instances of 'str' and 'int'
|
||||||
|
|||||||
+2
-2
@@ -408,7 +408,7 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
|||||||
"class_name": f"TestCase{testcase_cls_name}",
|
"class_name": f"TestCase{testcase_cls_name}",
|
||||||
"imports_list": imports_list,
|
"imports_list": imports_list,
|
||||||
"config_chain_style": make_config_chain_style(config),
|
"config_chain_style": make_config_chain_style(config),
|
||||||
"customization_test_start": make_test_start(config),
|
"customization_test_start": make_test_start_style(config),
|
||||||
"teststeps_chain_style": [
|
"teststeps_chain_style": [
|
||||||
make_teststep_chain_style(step) for step in teststeps
|
make_teststep_chain_style(step) for step in teststeps
|
||||||
],
|
],
|
||||||
@@ -610,7 +610,7 @@ def init_make_parser(subparsers):
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def make_test_start(config: Dict) -> Text:
|
def make_test_start_style(config: Dict) -> Text:
|
||||||
test_start_style = ""
|
test_start_style = ""
|
||||||
if config["parameters"]:
|
if config["parameters"]:
|
||||||
params = config["parameters"]
|
params = config["parameters"]
|
||||||
|
|||||||
@@ -490,13 +490,14 @@ def parse_parameters(parameters, variables_mapping=None, functions_mapping=None)
|
|||||||
>>> parse_parameters(parameters)
|
>>> parse_parameters(parameters)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from httprunner.loader import load_project_meta
|
|
||||||
variables_mapping = variables_mapping or {}
|
variables_mapping = variables_mapping or {}
|
||||||
functions_mapping = functions_mapping or {}
|
functions_mapping = functions_mapping or {}
|
||||||
parsed_parameters_list = []
|
parsed_parameters_list = []
|
||||||
# project_meta = load_project_meta("")
|
|
||||||
# functions_mapping.update(project_meta.functions)
|
# load project_meta functions
|
||||||
# logger.warning(f"functions_mapping: {functions_mapping}")
|
from httprunner.loader import load_project_meta
|
||||||
|
project_meta = load_project_meta("")
|
||||||
|
functions_mapping.update(project_meta.functions)
|
||||||
|
|
||||||
parameters = utils.ensure_mapping_format(parameters)
|
parameters = utils.ensure_mapping_format(parameters)
|
||||||
for parameter_name, parameter_content in parameters.items():
|
for parameter_name, parameter_content in parameters.items():
|
||||||
@@ -520,13 +521,12 @@ def parse_parameters(parameters, variables_mapping=None, functions_mapping=None)
|
|||||||
|
|
||||||
parameter_content_list.append(parameter_content_dict)
|
parameter_content_list.append(parameter_content_dict)
|
||||||
else:
|
else:
|
||||||
pass
|
|
||||||
# (2) & (3)
|
# (2) & (3)
|
||||||
parsed_variables_mapping = parse_variables_mapping(
|
parsed_variables_mapping = parse_variables_mapping(
|
||||||
variables_mapping,
|
variables_mapping,
|
||||||
functions_mapping
|
functions_mapping
|
||||||
)
|
)
|
||||||
parsed_parameter_content = parse_string(
|
parsed_parameter_content = parse_data(
|
||||||
parameter_content,
|
parameter_content,
|
||||||
parsed_variables_mapping,
|
parsed_variables_mapping,
|
||||||
functions_mapping
|
functions_mapping
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
username,password
|
||||||
|
test1,111111
|
||||||
|
test2,222222
|
||||||
|
@@ -9,6 +9,7 @@ from httprunner.make import (
|
|||||||
make_teststep_chain_style,
|
make_teststep_chain_style,
|
||||||
pytest_files_run_set,
|
pytest_files_run_set,
|
||||||
ensure_file_abs_path_valid,
|
ensure_file_abs_path_valid,
|
||||||
|
make_test_start_style
|
||||||
)
|
)
|
||||||
from httprunner import loader
|
from httprunner import loader
|
||||||
|
|
||||||
@@ -214,3 +215,26 @@ from request_methods.request_with_functions_test import (
|
|||||||
teststep_chain_style,
|
teststep_chain_style,
|
||||||
"""Step(RunRequest("get with params").with_variables(**{'foo1': 'bar1', 'foo2': 123, 'sum_v': '${sum_two(1, 2)}'}).get("/get").with_params(**{'foo1': '$foo1', 'foo2': '$foo2', 'sum_v': '$sum_v'}).with_headers(**{'User-Agent': 'HttpRunner/${get_httprunner_version()}'}).extract().with_jmespath('body.args.foo1', 'session_foo1').with_jmespath('body.args.foo2', 'session_foo2').validate().assert_equal("status_code", 200).assert_equal("body.args.sum_v", "3"))""",
|
"""Step(RunRequest("get with params").with_variables(**{'foo1': 'bar1', 'foo2': 123, 'sum_v': '${sum_two(1, 2)}'}).get("/get").with_params(**{'foo1': '$foo1', 'foo2': '$foo2', 'sum_v': '$sum_v'}).with_headers(**{'User-Agent': 'HttpRunner/${get_httprunner_version()}'}).extract().with_jmespath('body.args.foo1', 'session_foo1').with_jmespath('body.args.foo2', 'session_foo2').validate().assert_equal("status_code", 200).assert_equal("body.args.sum_v", "3"))""",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_make_test_start_style(self):
|
||||||
|
params = {
|
||||||
|
"user_agent": ["iOS/10.1", "iOS/10.2"],
|
||||||
|
"username-password": "${parameterize(request_methods/account.csv)}",
|
||||||
|
"app_version": "${get_app_version()}",
|
||||||
|
}
|
||||||
|
config = {
|
||||||
|
"parameters": params
|
||||||
|
}
|
||||||
|
self.assertEqual(
|
||||||
|
make_test_start_style(config),
|
||||||
|
f"""
|
||||||
|
import pytest
|
||||||
|
from httprunner.parser import parse_parameters
|
||||||
|
|
||||||
|
param = [{params}]
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('parametrize', parse_parameters(param))
|
||||||
|
def test_start(self, parametrize):
|
||||||
|
super().test_start(parametrize)
|
||||||
|
"""
|
||||||
|
)
|
||||||
@@ -452,3 +452,30 @@ class TestParserBasic(unittest.TestCase):
|
|||||||
self.assertEqual(parsed_testcase["headers"]["random"], variables["random"])
|
self.assertEqual(parsed_testcase["headers"]["random"], variables["random"])
|
||||||
self.assertEqual(parsed_testcase["body"], variables["data"])
|
self.assertEqual(parsed_testcase["body"], variables["data"])
|
||||||
self.assertEqual(parsed_testcase["headers"]["sum"], 3)
|
self.assertEqual(parsed_testcase["headers"]["sum"], 3)
|
||||||
|
|
||||||
|
def test_parse_parameters_testcase(self):
|
||||||
|
variables = {
|
||||||
|
"user_agent": "chrome",
|
||||||
|
"sum": 5,
|
||||||
|
}
|
||||||
|
param = [
|
||||||
|
{
|
||||||
|
"user_agent": ["iOS/10.1", "iOS/10.2"],
|
||||||
|
"username-password": "${parameterize(account.csv)}",
|
||||||
|
"sum": "${add_two_nums(1, 2)}",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
functions = {
|
||||||
|
"add_two_nums": lambda a, b=1: [a + b, b-a],
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed_params = parser.parse_parameters(param, variables, functions)
|
||||||
|
self.assertIn({'username': 'test1', 'password': '111111', 'user_agent': 'iOS/10.1', 'sum': 3}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test1', 'password': '111111', 'user_agent': 'iOS/10.1', 'sum': 1}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test1', 'password': '111111', 'user_agent': 'iOS/10.2', 'sum': 3}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test1', 'password': '111111', 'user_agent': 'iOS/10.2', 'sum': 1}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test2', 'password': '222222', 'user_agent': 'iOS/10.1', 'sum': 3}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test2', 'password': '222222', 'user_agent': 'iOS/10.1', 'sum': 1}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test2', 'password': '222222', 'user_agent': 'iOS/10.2', 'sum': 3}, parsed_params)
|
||||||
|
self.assertIn({'username': 'test2', 'password': '222222', 'user_agent': 'iOS/10.2', 'sum': 1}, parsed_params)
|
||||||
|
|||||||
@@ -125,3 +125,55 @@ class TestUtils(unittest.TestCase):
|
|||||||
override_config_variables(step_variables, config_variables),
|
override_config_variables(step_variables, config_variables),
|
||||||
{"base_url": "https://httpbin.org", "foo1": "bar1"},
|
{"base_url": "https://httpbin.org", "foo1": "bar1"},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_ensure_mapping_format(self):
|
||||||
|
map_list = [
|
||||||
|
{"a": 1},
|
||||||
|
{"b": 2}
|
||||||
|
]
|
||||||
|
ordered_dict = utils.ensure_mapping_format(map_list)
|
||||||
|
self.assertIsInstance(ordered_dict, dict)
|
||||||
|
self.assertIn("a", ordered_dict)
|
||||||
|
|
||||||
|
def test_cartesian_product_one(self):
|
||||||
|
parameters_content_list = [
|
||||||
|
[
|
||||||
|
{"a": 1},
|
||||||
|
{"a": 2}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
product_list = utils.gen_cartesian_product(*parameters_content_list)
|
||||||
|
self.assertEqual(
|
||||||
|
product_list,
|
||||||
|
[
|
||||||
|
{"a": 1},
|
||||||
|
{"a": 2}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_cartesian_product_multiple(self):
|
||||||
|
parameters_content_list = [
|
||||||
|
[
|
||||||
|
{"a": 1},
|
||||||
|
{"a": 2}
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{"x": 111, "y": 112},
|
||||||
|
{"x": 121, "y": 122}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
product_list = utils.gen_cartesian_product(*parameters_content_list)
|
||||||
|
self.assertEqual(
|
||||||
|
product_list,
|
||||||
|
[
|
||||||
|
{'a': 1, 'x': 111, 'y': 112},
|
||||||
|
{'a': 1, 'x': 121, 'y': 122},
|
||||||
|
{'a': 2, 'x': 111, 'y': 112},
|
||||||
|
{'a': 2, 'x': 121, 'y': 122}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_cartesian_product_empty(self):
|
||||||
|
parameters_content_list = []
|
||||||
|
product_list = utils.gen_cartesian_product(*parameters_content_list)
|
||||||
|
self.assertEqual(product_list, [])
|
||||||
Reference in New Issue
Block a user