Introduction
The author, after being very busy at work, decided to take time to record the problems encountered when testing the interface for future reference. It can also be provided to those partners who are learning interface testing to avoid detours. If it helps you, give it a like, thank you.
Today’s article mainly talks about the processing of request parameters containing escape characters and return parameters containing escape characters in interface testing.
1. First, take a look at the data style of the parameters in my interface:
There are two parameters in this body: “body” and “method”. The entire data variable is a dictionary, but “body” is a string and contains escape characters. The entire data variable is an escape string. There is no problem in writing this kind of parameter in the code, but when used as an interface request, sometimes it cannot be parsed by json, which ultimately leads to the failure of the interface request.
This parameter is not processed and is requested directly, as shown in the figure below:
2. The target has been determined, and the next step is to process it. I first de-escape the data. It is actually very simple. I use a json method loads(), as shown in the figure:
When using this method, you need to process the data in advance because the types supported by this method are limited, such as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
I first convert data into str, as shown in the figure:
The reason why I posted a screenshot of the error report is that I have a knowledge point to teach you.
Python strings are hierarchical, such as using ”’ ”’ and ” ” and ‘ ‘, so you cannot use two ” ” like the picture above.
After modification, execute the calling interface program:
This return result is what I want.
Now that we’ve talked about the escaping of input parameters, what about the external parameters? Leave it to everyone to think.
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected. If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up. You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc. Share their experience, and also share many live lectures and technical salons You can learn for free! Focus on it! Open source! ! ! QQ group number: 110685036
Encoding processing
Many times, the data returned includes Chinese and binary data. Let’s first look at the unprocessed data returned by the interface, which is shown as follows:
This kind of data is firstly inconvenient to view, and secondly it is difficult to find the value you want.
1 |
|
Run the script:
1 |
|
What this sentence says is that gbk cannot encode, but my code encoding is utf-8, which is obviously not a code problem. The error location at ‘\xe2’ cannot be decoded. Add the standard output code:
1 2 3 |
|
Execute the program again and the result shows success:
It should be noted that if gb18030 does not work, use utf-8, such as:
1 |
|
It can also be changed to:
1 |
|
Below are some corresponding encoding tables for Chinese:
Encoding name | Purpose |
utf8 | All languages |
gbk | Simplified Chinese |
gb2312 | Simplified Chinese |
gb18030 | Simplified Chinese |
big5 | Traditional Chinese |
big5hkscs | Traditional Chinese |
Reason explanation: For Unicode characters, if you need to print them out, since the local system is cmd in Windows, the default codepage is CP936, which is the encoding of GBK, so the python interpreter needs to encode the above Unicode characters into GBK first, and then in cmd displayed in . However, because the Unicode string contains some characters that cannot be displayed in GBK, the error “‘gbk’ codec can’t encode” is prompted at this time. In fact, the limitation of the print() function is the limitation of Python’s default encoding. Because the system is Windows, the default encoding of python is not ‘utf-8’. Just change the default encoding of python to ‘utf-8’.
Interface request method
As mentioned in the introduction, if you don’t know about requests, you can refer to my article Python interface to test the difference between data and json parameters in the requests.post method. This article talks about the two data types data and json for post requests. So how to use these two parameter types to request data with escape characters in the body data at the same time? Before speaking, let’s review some knowledge points:
1 2 3 4 5 6 7 |
|
Specific script:
The result is as follows:
Regardless of whether you choose the data type or the json type, as long as the types of parameters passed correspond to each other, there will be no problem. There is no script code directly posted here. It is also for beginners to type by themselves. They cannot be directly copied and used, which will hinder their own coding improvement.
Extract parameters from the message
How to get the values corresponding to the key in the returned result. For example, with this interface, I want to get the value of the warehouseName field, as shown in the figure:
From the data point of view, the returned data type is a dictionary, and the dictionary warehouseName I want to obtain is in the dictionary data. data is a dictionary. There is also a list in it. There is also a dictionary in the list, which is equivalent to 4 levels of nesting. How? What about taking out the median value of the 4th layer? This is a two-step operation, please see the code for details:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
The output results are as follows:
Appendix
1. Here are the knowledge points about (u, r, b in front of the string) to deepen your impression. If you haven’t understood it before, just learn it and you will naturally use it in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three |
The string following
Hello World!'
|
2. Regarding the basic knowledge of python encoding, the conversion between string and byte stream is as follows:
Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!
Software testing interview document
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill tree Home page Overview 379,410 people are learning the system