Home > Article > Backend Development > Practical summary of using Python to quickly build interface automated test scripts
Usually, in our projects, our interface test requirements are generally to construct different request data, and then send the request to the interface. After getting the interface return, we will The fields are extracted and verified, and finally the results are stored in an excel table for easy reference. The interface is generally an http or https request, and the structure sent is generally json body or json combined with some file attachments. The return results of the request are all in json format. Our test cases can be saved in Excel or database, and the results can be saved in the database or exist directly. In Excel, the following will specifically dismantle the requirements and introduce the implementation process step by step.
Constructing the request content sent each time and automatically sending the request to the interface is the core of constructing the automated test script. We mainly use python to implement this step requests library, let’s give a detailed introduction below.
Before sending the request, we need to clarify the body of the request. Our body is json. The specific content is as follows:
We can save this as a text.json file as a template, which can be read directly to prepare for later construction of the request body. We can handle this step like this. With the help of the yaml package, we can convert json into a dictionary, or we can use the json that comes with python, and the effect is the same.
After obtaining the request body template, we get the variable request_body, which is a dictionary type data. We can parameterize it to construct what we need The request body. For example, if we want to modify the request id, user name, and text content of each request, we can do this. The left side is the field that needs to be modified, and the right side is the variable we need.
After constructing the data to be sent, you can prepare to send the request. Before sending the request, we still have some work to do, which is to set some parameters of the request interface and Some customization of request headers, here we give a simple example as follows:
# After we customize the request parameters and request headers, we can send a request similar to the following URL:
We add the previously constructed body, and then use the post method of the requests library to send the request. The data parameter in the method is used here. It receives a json, so when sending Previously, the previous dictionary variables needed to be converted before sending. Here we use the json library that comes with Python, and use the dumps method to convert the dictionary to json:
At this point, a basic http post request has been sent. Note that we have a Response object named r. We can get all the information we want from this object.
We introduced the simplest http post request earlier. On this basis, we sometimes need some more complex requests, such as bringing a file, https Request, etc. Here is a brief explanation of how to implement it:
For example, we want to send an audio file with the format pcm to the interface, and the interface is https.
Note that sending https requests requires SSL authentication. Use the verify parameter in the method. The default value of this parameter is True. Generally, if verification is not required, you need to set this is False. Another thing to note is that we set a timeout to prevent the request process from timing out and causing the program to become unresponsive.
In the step of sending the request, we have a Response object named r. We can get all the information we want from this object.
There are several methods to obtain content, which we can use according to our own needs:
The text obtained is generally in json format:
We can convert json and use the json.loads method to convert a json object into a python dictionary, so that we can easily obtain some of the fields we want. This step is very simple. , I won’t introduce it in detail.
First let’s take a look at our case. Our case is written using Excel, as follows:
How to read Excel and get the cases? We used the pandas library in python. This library is very powerful and has many methods for processing data. We only use the method of reading excel. The specific code is as follows:
In this way we convert the table data A list is created, and each list is in a dictionary format, which is our case. The specific format is as follows:
The purpose of this is that we can combine the header and each Each case is mapped to form a dictionary, which allows for more flexible case operation and data comparison.
With the case list and the previous steps of sending requests and obtaining results, we can perform batch interface testing. Here, we can use a for loop to run in batches:
We splice the results returned each time into a dictionary according to the case format, which is our result data. We store each result dictionary into a list to get the entire result dictionary list. , we name it case_result_list. At this time, we use the pandas library again to convert this list into dataframe format:
After that we save the dataframe as an excel file:
So far, we have completed the entire process from obtaining the case to sending a request to obtain the results and saving the results.
After the above operations, we have completed the process of sending requests in batches and obtaining results. If we need to do some processing on the result cells, such as marking them red Boldening and other operations can make the error information in the test results more obvious. What should be done? Here we use the openpyxl library in python. This library can also read and write Excel tables, and can insert some formulas and styles. What we use here is style operation. We highlight the results in red and bold according to the data in the cell:
The final test results obtained are as follows, by using openpyxl , we can also append rows to the results to add some statistical information of the test results, such as the number of cases, the number of errors, the error rate and the accuracy rate, etc.
The above is a complete interface automated test script to implement automatic request, obtain results, data comparison analysis, export results to Excel, etc. Function, each step is relatively simple, you can quickly build an automated test script that meets your needs, and quickly verify the server interface.
The requests library and pandas library used are commonly used libraries in python, and their functions are very powerful. You can refer to their official documents for in-depth understanding later.
The above is the detailed content of Practical summary of using Python to quickly build interface automated test scripts. For more information, please follow other related articles on the PHP Chinese website!