save api in individual file: each file is corresponding to one api definition

This commit is contained in:
debugtalk
2018-12-04 22:38:41 +08:00
parent 0cfb92aa4a
commit 0eacd5f7de
13 changed files with 150 additions and 14 deletions

View File

@@ -281,6 +281,7 @@ def load_debugtalk_functions():
project_mapping = {} project_mapping = {}
tests_def_mapping = { tests_def_mapping = {
"PWD": None,
"api": {}, "api": {},
"testcases": {} "testcases": {}
} }
@@ -323,6 +324,16 @@ def load_test(raw_testinfo):
# reference api # reference api
if "api" in raw_testinfo: if "api" in raw_testinfo:
api_name = raw_testinfo["api"] api_name = raw_testinfo["api"]
# api maybe defined in two types:
# 1, individual file: each file is corresponding to one api definition
# 2, api sets file: one file contains a list of api definitions
if not os.path.isabs(api_name):
api_path = os.path.join(tests_def_mapping["PWD"], api_name)
if os.path.isfile(api_path):
# type 1: api is defined in individual file
api_name = api_path
raw_testinfo["api_def"] = _get_api_definition(api_name) raw_testinfo["api_def"] = _get_api_definition(api_name)
# TODO: reference proc functions # TODO: reference proc functions
@@ -495,22 +506,26 @@ def load_api_folder(api_folder_path):
} }
""" """
# TODO: refactor api storage format, use one file for each api.
api_definition_mapping = {} api_definition_mapping = {}
api_items_mapping = load_folder_content(api_folder_path) api_items_mapping = load_folder_content(api_folder_path)
for api_file_path, api_items in api_items_mapping.items(): for api_file_path, api_items in api_items_mapping.items():
# TODO: add JSON schema validation # TODO: add JSON schema validation
for api_item in api_items: if isinstance(api_items, list):
key, api_dict = api_item.popitem() for api_item in api_items:
key, api_dict = api_item.popitem()
api_id = api_dict.get("id")
if api_id in api_definition_mapping:
logger.log_warning("API definition duplicated: {}".format(api_id))
# TODO: replace id with api file path api_definition_mapping[api_id] = api_dict
api_id = api_dict.get("id")
if api_id in api_definition_mapping:
logger.log_warning("API definition duplicated: {}".format(api_id))
api_definition_mapping[api_id] = api_dict elif isinstance(api_items, dict):
if api_file_path in api_definition_mapping:
logger.log_warning("API definition duplicated: {}".format(api_file_path))
api_definition_mapping[api_file_path] = api_items
return api_definition_mapping return api_definition_mapping
@@ -579,6 +594,7 @@ def load_project_tests(test_path, dot_env_path=None):
# load api # load api
tests_def_mapping["api"] = load_api_folder(os.path.join(project_working_directory, "api")) tests_def_mapping["api"] = load_api_folder(os.path.join(project_working_directory, "api"))
tests_def_mapping["PWD"] = project_working_directory
def load_tests(path, dot_env_path=None): def load_tests(path, dot_env_path=None):

View File

@@ -2,7 +2,6 @@
id: get_token id: get_token
name: get token name: get token
variables: variables:
user_agent: XXX user_agent: XXX
device_sn: API_XXX device_sn: API_XXX
os_platform: XXX os_platform: XXX

17
tests/api/create_user.yml Normal file
View File

@@ -0,0 +1,17 @@
variables:
user_name: user0
user_password: "000000"
uid: 9000
token: XXX
request:
url: /api/users/$uid
method: POST
headers:
Content-Type: "application/json"
device_sn: $device_sn
token: $token
json:
name: $user_name
password: $user_password
validate:
- eq: ["status_code", 201]

12
tests/api/delete_user.yml Normal file
View File

@@ -0,0 +1,12 @@
variables:
uid: 9000
token: XXX
request:
url: /api/users/$uid
method: DELETE
headers:
Content-Type: "application/json"
device_sn: $device_sn
token: $token
validate:
- eq: ["status_code", 200]

16
tests/api/get_headers.yml Normal file
View File

@@ -0,0 +1,16 @@
variables:
n_secs: 1
request:
url: /headers
headers:
Content-Type: "application/json"
device_sn: $device_sn
method: GET
setup_hooks:
- ${setup_hook_add_kwargs($request)}
- ${setup_hook_remove_kwargs($request)}
teardown_hooks:
- ${teardown_hook_sleep_N_secs($response, $n_secs)}
validate:
- eq: ["status_code", 200]
- contained_by: [content.headers.Host, "${get_httpbin_server()}"]

22
tests/api/get_token.yml Normal file
View File

@@ -0,0 +1,22 @@
name: get token
variables:
user_agent: XXX
device_sn: API_XXX
os_platform: XXX
app_version: XXX
request:
url: /api/get-token
method: POST
headers:
user_agent: $user_agent
device_sn: $device_sn
os_platform: $os_platform
app_version: $app_version
Content-Type: "application/json"
device_sn: $device_sn
json:
sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)}
validate:
- eq: ["status_code", 0]
- len_eq: ["content.token", 12]
- contains: [{"a": 1, "b": 2}, "a"]

12
tests/api/get_user.yml Normal file
View File

@@ -0,0 +1,12 @@
variables:
uid: 9000
token: XXX
request:
url: /api/users/$uid
method: GET
headers:
Content-Type: "application/json"
device_sn: $device_sn
token: $token
validate:
- eq: ["status_code", 200]

11
tests/api/get_users.yml Normal file
View File

@@ -0,0 +1,11 @@
variables:
token: XXX
request:
url: /api/users
method: GET
headers:
Content-Type: "application/json"
device_sn: $device_sn
token: $token
validate:
- eq: ["status_code", 200]

12
tests/api/reset_all.yml Normal file
View File

@@ -0,0 +1,12 @@
variables:
token: XXX
request:
url: /api/reset-all
method: GET
headers:
Content-Type: "application/json"
device_sn: $device_sn
token: $token
validate:
- eq: ["status_code", 200]
- eq: ["content.success", true]

17
tests/api/update_user.yml Normal file
View File

@@ -0,0 +1,17 @@
variables:
user_name: user0
user_password: "000000"
uid: 9000
token: XXX
request:
url: /api/users/$uid
method: PUT
headers:
Content-Type: "application/json"
device_sn: $device_sn
token: $token
json:
name: $user_name
password: $user_password
validate:
- eq: ["status_code", 200]

View File

@@ -439,6 +439,8 @@ class TestSuiteLoader(unittest.TestCase):
api_definition_mapping = loader.load_api_folder(path) api_definition_mapping = loader.load_api_folder(path)
self.assertIn("get_token", api_definition_mapping) self.assertIn("get_token", api_definition_mapping)
self.assertIn("request", api_definition_mapping["get_token"]) self.assertIn("request", api_definition_mapping["get_token"])
api_path = os.path.join(os.getcwd(), "tests", "api", "get_token.yml")
self.assertIn(api_path, api_definition_mapping)
def test_load_project_tests(self): def test_load_project_tests(self):
loader.load_project_tests(os.path.join(os.getcwd(), "tests")) loader.load_project_tests(os.path.join(os.getcwd(), "tests"))

View File

@@ -15,7 +15,7 @@
- test: - test:
name: make sure user $uid does not exist name: make sure user $uid does not exist
api: get_user api: api/get_user.yml
variables: variables:
uid: $uid uid: $uid
token: $token token: $token
@@ -25,7 +25,7 @@
- test: - test:
name: create user $uid name: create user $uid
api: create_user api: api/create_user.yml
variables: variables:
user_name: "user1" user_name: "user1"
user_password: "123456" user_password: "123456"
@@ -37,7 +37,7 @@
- test: - test:
name: check if user $uid exists name: check if user $uid exists
api: get_user api: api/get_user.yml
variables: variables:
uid: $uid uid: $uid
token: $token token: $token

View File

@@ -13,7 +13,7 @@
- test: - test:
name: get token (setup) name: get token (setup)
api: get_token api: api/get_token.yml
variables: variables:
user_agent: 'iOS/10.3' user_agent: 'iOS/10.3'
device_sn: $device_sn device_sn: $device_sn
@@ -27,6 +27,6 @@
- test: - test:
name: reset all users name: reset all users
api: reset_all api: api/reset_all.yml
variables: variables:
token: $token token: $token