test: #931 add test for parameters

This commit is contained in:
jun
2020-07-02 18:11:11 +08:00
parent 28dadc38dc
commit 8c65cfe45e
7 changed files with 119 additions and 14 deletions

3
tests/account.csv Normal file
View File

@@ -0,0 +1,3 @@
username,password
test1,111111
test2,222222
1 username password
2 test1 111111
3 test2 222222

View File

@@ -9,6 +9,7 @@ from httprunner.make import (
make_teststep_chain_style,
pytest_files_run_set,
ensure_file_abs_path_valid,
make_test_start_style
)
from httprunner import loader
@@ -214,3 +215,26 @@ from request_methods.request_with_functions_test import (
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"))""",
)
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)
"""
)

View File

@@ -452,3 +452,30 @@ class TestParserBasic(unittest.TestCase):
self.assertEqual(parsed_testcase["headers"]["random"], variables["random"])
self.assertEqual(parsed_testcase["body"], variables["data"])
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)

View File

@@ -125,3 +125,55 @@ class TestUtils(unittest.TestCase):
override_config_variables(step_variables, config_variables),
{"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, [])