Home  >  Article  >  Backend Development  >  Practical summary of using Python to quickly build interface automated test scripts

Practical summary of using Python to quickly build interface automated test scripts

王林
王林forward
2023-04-11 22:28:051806browse

​Introduction to test requirements

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.

Customizing the request body and sending the request

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.

1. Send a simple http post request

Before sending the request, we need to clarify the body of the request. Our body is json. The specific content is as follows:

Practical summary of using Python to quickly build interface automated test scripts

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.

Practical summary of using Python to quickly build interface automated test scripts

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.

Practical summary of using Python to quickly build interface automated test scripts

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:

Practical summary of using Python to quickly build interface automated test scripts

# After we customize the request parameters and request headers, we can send a request similar to the following URL:

Practical summary of using Python to quickly build interface automated test scripts

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:

Practical summary of using Python to quickly build interface automated test scripts

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.

2. More complex requests

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.

Practical summary of using Python to quickly build interface automated test scripts

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.

Capture key data from the request return data

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:

Practical summary of using Python to quickly build interface automated test scripts

The text obtained is generally in json format:

Practical summary of using Python to quickly build interface automated test scripts

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.

How to execute case and store test results

First let’s take a look at our case. Our case is written using Excel, as follows:

Practical summary of using Python to quickly build interface automated test scripts

1080×112 39.8 KB

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:

Practical summary of using Python to quickly build interface automated test scripts

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:

Practical summary of using Python to quickly build interface automated test scripts

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:

Practical summary of using Python to quickly build interface automated test scripts

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:

Practical summary of using Python to quickly build interface automated test scripts

After that we save the dataframe as an excel file:

Practical summary of using Python to quickly build interface automated test scripts

So far, we have completed the entire process from obtaining the case to sending a request to obtain the results and saving the results.

Identify the result data

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:

Practical summary of using Python to quickly build interface automated test scripts

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.

Practical summary of using Python to quickly build interface automated test scripts

1080×224 61.6 KB

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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete