mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-16 22:27:36 +08:00
new feature: support parameters and data driven
This commit is contained in:
4
tests/data/app_version.csv
Normal file
4
tests/data/app_version.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
app_version
|
||||
2.8.5
|
||||
2.8.6
|
||||
|
||||
|
26
tests/data/demo_parameters.yml
Normal file
26
tests/data/demo_parameters.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
- config:
|
||||
name: "user management testset."
|
||||
parameters:
|
||||
- user_agent: Random
|
||||
- app_version: Sequential
|
||||
variables:
|
||||
- user_agent: 'iOS/10.3'
|
||||
- device_sn: ${gen_random_string(15)}
|
||||
- os_platform: 'ios'
|
||||
- app_version: '2.8.6'
|
||||
request:
|
||||
base_url: $BASE_URL
|
||||
headers:
|
||||
Content-Type: application/json
|
||||
device_sn: $device_sn
|
||||
output:
|
||||
- token
|
||||
|
||||
- test:
|
||||
name: get token with $user_agent and $app_version
|
||||
api: get_token($user_agent, $device_sn, $os_platform, $app_version)
|
||||
extract:
|
||||
- token: content.token
|
||||
validate:
|
||||
- "eq": ["status_code", 200]
|
||||
- "len_eq": ["content.token", 16]
|
||||
4
tests/data/user_agent.csv
Normal file
4
tests/data/user_agent.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
user_agent
|
||||
iOS/10.1
|
||||
iOS/10.2
|
||||
iOS/10.3
|
||||
|
4
tests/data/username-password.csv
Normal file
4
tests/data/username-password.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
username,password
|
||||
test1,111111
|
||||
test2,222222
|
||||
test3,333333
|
||||
|
@@ -158,3 +158,11 @@ class TestRunner(ApiServerUnittest):
|
||||
|
||||
test = testcases[2]["test"]
|
||||
self.assertTrue(self.test_runner._run_test(test))
|
||||
|
||||
def test_run_testset_with_parameters(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_parameters.yml')
|
||||
result = run_suite_path(testcase_file_path)
|
||||
self.assertTrue(result.success)
|
||||
self.assertIn("token", result.output)
|
||||
self.assertEqual(result.stat.total, 6)
|
||||
|
||||
@@ -3,14 +3,16 @@ import time
|
||||
import unittest
|
||||
|
||||
from httprunner import testcase
|
||||
from httprunner.exception import ApiNotFound, FileFormatError, ParamsError
|
||||
from httprunner.exception import (ApiNotFound, FileFormatError,
|
||||
FileNotFoundError, ParamsError)
|
||||
|
||||
|
||||
class TestcaseParserUnittest(unittest.TestCase):
|
||||
|
||||
def test_load_testcases_bad_filepath(self):
|
||||
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
|
||||
self.assertEqual(testcase.load_file(testcase_file_path), [])
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
testcase.load_file(testcase_file_path)
|
||||
|
||||
def test_load_json_testcases(self):
|
||||
testcase_file_path = os.path.join(
|
||||
@@ -34,6 +36,111 @@ class TestcaseParserUnittest(unittest.TestCase):
|
||||
self.assertIn('url', test['request'])
|
||||
self.assertIn('method', test['request'])
|
||||
|
||||
def test_load_csv_file_one_parameter(self):
|
||||
csv_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/user_agent.csv')
|
||||
csv_content = testcase.load_file(csv_file_path)
|
||||
self.assertEqual(
|
||||
csv_content,
|
||||
[
|
||||
{'user_agent': 'iOS/10.1'},
|
||||
{'user_agent': 'iOS/10.2'},
|
||||
{'user_agent': 'iOS/10.3'}
|
||||
]
|
||||
)
|
||||
|
||||
def test_load_csv_file_multiple_parameters(self):
|
||||
csv_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/username-password.csv')
|
||||
csv_content = testcase.load_file(csv_file_path)
|
||||
self.assertEqual(
|
||||
csv_content,
|
||||
[
|
||||
{'username': 'test1', 'password': '111111'},
|
||||
{'username': 'test2', 'password': '222222'},
|
||||
{'username': 'test3', 'password': '333333'}
|
||||
]
|
||||
)
|
||||
|
||||
def test_cartesian_product_one(self):
|
||||
parameters_content_list = [
|
||||
[
|
||||
{"a": 1},
|
||||
{"a": 2}
|
||||
]
|
||||
]
|
||||
product_list = testcase.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 = testcase.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 = testcase.gen_cartesian_product(*parameters_content_list)
|
||||
self.assertEqual(product_list, [])
|
||||
|
||||
def test_gen_cartesian_product_parameters_one_to_one(self):
|
||||
parameters = [
|
||||
{"user_agent": "random"},
|
||||
{"app_version": "sequential"}
|
||||
]
|
||||
testset_path = os.path.join(
|
||||
os.getcwd(),
|
||||
"tests/data/demo_parameters.yml"
|
||||
)
|
||||
cartesian_product_parameters = testcase.gen_cartesian_product_parameters(
|
||||
parameters,
|
||||
testset_path
|
||||
)
|
||||
self.assertEqual(
|
||||
len(cartesian_product_parameters),
|
||||
6
|
||||
)
|
||||
|
||||
def test_gen_cartesian_product_parameters_one_to_multiple(self):
|
||||
parameters = [
|
||||
{"user_agent": "random"},
|
||||
{"username-password": "sequential"}
|
||||
]
|
||||
testset_path = os.path.join(
|
||||
os.getcwd(),
|
||||
"tests/data/demo_parameters.yml"
|
||||
)
|
||||
cartesian_product_parameters = testcase.gen_cartesian_product_parameters(
|
||||
parameters,
|
||||
testset_path
|
||||
)
|
||||
self.assertEqual(
|
||||
len(cartesian_product_parameters),
|
||||
9
|
||||
)
|
||||
|
||||
def test_load_yaml_file_file_format_error(self):
|
||||
yaml_tmp_file = "tests/data/tmp.yml"
|
||||
# create empty yaml file
|
||||
|
||||
Reference in New Issue
Block a user