Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python realize that the data returned by the interface test is a dictionary value?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article Xiaobian for you to introduce in detail "python how to achieve interface test return data for the dictionary value method", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to realize the interface test return data for dictionary value mode" article can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

The data returned by the interface test is a dictionary value.

API testing usually needs to verify whether the returned data is consistent with the expected result. If the returned data is a dictionary, then when we want to get the values corresponding to our desired key, we need to skillfully use dict.keys (), dict.values () and for loop, as well as list related knowledge points.

Example

This is the data returned by my calling API, which is of type dict, and my goal is to get the account.

# data returned by API #: api_result = {'code':' 000001, 'dataMap': {' data': {'amount': 0,' billingWeight': 0, 'quantity': 0}},' failureString':', 'failures': [],' flag': 'success',' hasError': False, 'message':' request succeeded'}

Method

# method 1:print ("this is method 1") for i in api_result.keys (): if I = = 'dataMap': print (api_ result [I] [' data'] ['amount']) # method 2:print ("this is method 2") getkey = api_result.get (' dataMap') # print (getkey) cc = list (getkey.values ()) [0] print (cc ['amount']) # method 3:print ("this Is method 3 ") print (api_result ['dataMap'] [' data'] ['amount'])

Running result

This time, I introduced three methods, a loop, one using keys () and values (), and the other directly nesting the corresponding value of key. Of course, you can see that the last method is the fastest and easiest.

Python Interface Test-- sign signature

Recently, the project tested needs to be called by a third party, so security authentication is added, and all interface calls need to be signed for signature verification.

So I studied the use of python to generate sign values according to the interface signature specification.

Interface signature specification

1. After the sign parameter and the empty parameter in the request parameters are removed, the remaining key-value pairs will be sorted in lexicographic order.

And spell a string in key1=value1&key2=value2 format

2. URLEncode is required for stitching strings.

3. Concatenate the developer's key and get the string to be signed after the sorted string in the first step

4. Use the md5 algorithm to encrypt the string to be encrypted and convert it to uppercase, which is sign.

Implementation code

The following is the code chip of the implementation.

Import timeimport requestsimport jsonimport urllib,hashliburl = "http://www.xxx.xx"# gets the current timestamp stamp = time.time () print (stamp) # 13-bit timestamp converted to int type timestam = int (round (stamp*1000)) print (timestam) time_new = str (timestam) print (type (time_new)) data = {'appId':'3301820001'," outTradeNo ":" 20201026001 "," amount ":" 100th "," body ":" test " "timestamp": time_new, "notifyUrl": "http://localhost:8080/notify",}# sorts the keys in lexicographical order data_order = sorted (data.items (), key=lambda XRX [0]) Reverse=False) # convert the list into a dictionary data_new = dict (data_order) print (data_new) # encode key=value key-value pairs datanew = urllib.parse.urlencode (data_new) datadic = datanew + "& key=BF1BDE5A649724056F904A9335B1C1C7455655" print (datadic) # create md5 object m = hashlib.md5 () m = hashlib.md5 (datadic.encode () data_md5 = m.hexdigest () # switch lowercase letters to uppercase sign=data_md5.upper () print (sign) here This article "how to implement python interface test return data to take the value of the dictionary" article has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to learn more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report