mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-15 12:27:59 +08:00
new feature: testcases can be layered, now we can define interface in api block individually
This commit is contained in:
26
tests/data/api.yml
Normal file
26
tests/data/api.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
- api:
|
||||
def: get_token($user_name, $device_sn, $os_platform, $app_version)
|
||||
request:
|
||||
url: /api/get-token
|
||||
method: POST
|
||||
headers:
|
||||
user_agent: $user_agent
|
||||
device_sn: $device_sn
|
||||
os_platform: $os_platform
|
||||
app_version: $app_version
|
||||
json:
|
||||
sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)}
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 200}
|
||||
- {"check": "content.token", "comparator": "len_eq", "expected": 16}
|
||||
|
||||
- api:
|
||||
def: create_user($uid, $user_name, $user_password, $token)
|
||||
request:
|
||||
url: /api/users/$uid
|
||||
method: POST
|
||||
headers:
|
||||
token: $token
|
||||
json:
|
||||
name: $user_name
|
||||
password: $user_password
|
||||
40
tests/data/demo_testset_layer.yml
Normal file
40
tests/data/demo_testset_layer.yml
Normal file
@@ -0,0 +1,40 @@
|
||||
- config:
|
||||
name: "create user testsets."
|
||||
variable_binds:
|
||||
- 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
|
||||
|
||||
- test:
|
||||
name: get token
|
||||
api: get_token($user_agent, $device_sn, $os_platform, $app_version)
|
||||
extract_binds:
|
||||
- token: content.token
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- uid: 1000
|
||||
- user_name: "user1"
|
||||
- user_password: "123456"
|
||||
api: create_user($uid, $user_name, $user_password, $token)
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 201}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": true}
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- uid: 1000
|
||||
- user_name: "user1"
|
||||
- user_password: "123456"
|
||||
api: create_user($uid, $user_name, $user_password, $token)
|
||||
validators:
|
||||
- {"check": "status_code", "comparator": "eq", "expected": 500}
|
||||
- {"check": "content.success", "comparator": "eq", "expected": false}
|
||||
@@ -107,3 +107,11 @@ class TestRunner(ApiServerUnittest):
|
||||
results = self.test_runner.run_testsets(testsets)
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results, [[True] * 3])
|
||||
|
||||
def test_run_testset_layered(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_testset_layer.yml')
|
||||
testsets = utils.load_testcases_by_path(testcase_file_path)
|
||||
results = self.test_runner.run_testsets(testsets)
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results, [[True] * 3])
|
||||
|
||||
@@ -44,7 +44,8 @@ class TestUtils(ApiServerUnittest):
|
||||
self.assertNotIn(file1, files)
|
||||
|
||||
files = utils.load_folder_files(folder, file_type="api", recursive=True)
|
||||
self.assertEqual(files, [])
|
||||
api_file = os.path.join(os.getcwd(), 'tests', 'data', 'api.yml')
|
||||
self.assertEqual(files[0], api_file)
|
||||
|
||||
def test_load_testcases_by_path_files(self):
|
||||
testsets_list = []
|
||||
@@ -125,6 +126,36 @@ class TestUtils(ApiServerUnittest):
|
||||
testset_list_3 = utils.load_testcases_by_path(path)
|
||||
self.assertEqual(testset_list_3, [])
|
||||
|
||||
def test_load_testcases_by_path_layered(self):
|
||||
path = os.path.join(
|
||||
os.getcwd(), 'tests/data/demo_testset_layer.yml')
|
||||
testsets_list = utils.load_testcases_by_path(path)
|
||||
self.assertIn("variable_binds", testsets_list[0]["config"])
|
||||
self.assertIn("request", testsets_list[0]["config"])
|
||||
print(testsets_list[0]["testcases"][0])
|
||||
self.assertIn("request", testsets_list[0]["testcases"][0])
|
||||
self.assertIn("url", testsets_list[0]["testcases"][0]["request"])
|
||||
self.assertIn("validators", testsets_list[0]["testcases"][0])
|
||||
|
||||
def test_load_api_definition(self):
|
||||
path = os.path.join(
|
||||
os.getcwd(), 'tests/data')
|
||||
api_dir_dict = utils.load_api_definition(path)
|
||||
self.assertIn("get_token", api_dir_dict)
|
||||
self.assertEqual("/api/get-token", api_dir_dict["get_token"]["request"]["url"])
|
||||
self.assertIn("$user_name", api_dir_dict["get_token"]["function_meta"]["args"])
|
||||
self.assertIn("create_user", api_dir_dict)
|
||||
|
||||
def test_get_api_definition(self):
|
||||
path = os.path.join(
|
||||
os.getcwd(), 'tests/data')
|
||||
api_info = utils.get_api_definition("get_token", path)
|
||||
self.assertEqual("/api/get-token", api_info["request"]["url"])
|
||||
self.assertIn(path, utils.api_overall_dict)
|
||||
|
||||
with self.assertRaises(exception.ApiNotFound):
|
||||
utils.get_api_definition("api_not_exist", path)
|
||||
|
||||
def test_query_json(self):
|
||||
json_content = {
|
||||
"ids": [1, 2, 3, 4],
|
||||
|
||||
Reference in New Issue
Block a user