python interface to test the return value data dictionary

introduction

Interface testing is usually required to return the check if the data is consistent with the expected results, this time if the data is returned to the dictionary, then we want to get our key corresponding values, to be clever use dict.keys (), dict.values () and a for loop, and a list of relevant knowledge points.

Examples

This is my tune interface to the data returned, the data type dict, my goal is to get the account.

#接口返回的数据:
api_result = {'code': '000001', 
'dataMap': {'data': 
{'amount': 0, 'billingWeight': 0, 'quantity': 0}},
 'failureString': '', 'failures': [], 
 'flag': 'success', 'hasError': False, 'message': '请求成功'}

method

#方法1print("这是方法1")
for i in api_result.keys():
    if i == 'dataMap':
        print(api_result[i]['data']['amount'])

#方法2print("这是方法2")
getkey = api_result.get('dataMap')
# print(getkey)
cc =list(getkey.values())[0]
print(cc['amount'])


#方法3print("这是方法3")
print(api_result['dataMap']['data']['amount'])

operation result

Here Insert Picture Description

This describes three methods, a loop, a use of keys () and values ​​(), a further nested directly take a value corresponding key. Of course you can see, the last one is the fastest and easiest method.

Published 82 original articles · won praise 43 · views 180 000 +

Guess you like

Origin blog.csdn.net/liudinglong1989/article/details/103260828