Use the Python interface to automatically test post requests and get requests, and obtain request return values

Table of contents

introduction

When the request interface is Post, the parameter passing method

 Get interface request response data


introduction

When we are doing automated testing of python interfaces, the interface request methods include get, post, etc.; get and post request parameters, and methods for obtaining interface response data;

When the request interface is Post, the parameter passing method

When we use the requests library in python for interface testing, we need to pass in the request body when doing the post interface test to pass parameters. We define the request body as a dictionary type containing key and value values, as follows

Python interface automation test video tutorial: 2023 latest collection of Python automation test development framework [full stack/actual combat/tutorial] collection essence, annual salary 40W+ after learning_哔哩哔哩_bilibili icon-default.png?t=N3I4https://www.bilibili.com/video/ BV1AF411T7qJ/?spm_id_from=333.999.0.0

 

Then when the request is made, the data will be assigned to the post request, but this is wrong. The data type of data is dict, and the data type of the interface request is string, which is just a string in json format, but the essence is that the string is It will not change, as shown in the figure below, if you use the data parameter directly, it will prompt that the parameter passing fails, and there is no request body;

Under normal circumstances, when using request to pass parameters, data or json will be passed in. Under what circumstances, data or json will be passed in. We check its Content-Type type in the interface document or system interface request. As shown in the figure below, we check the login interface as : application/json, in json format, use json parameter

Method 1: convert it into a string in json format through json.dunps(body), and then pass it to data;

Method 2: When passing parameters, directly format the parameters in json format:

Video Tutorial:

2023 latest collection of Python automated testing development framework [full stack/actual combat/tutorial] collection essence, annual salary 40W+ after learning _哔哩哔哩_bilibili https://www.bilibili.com/video/BV1AF411T7qJ/?spm_id_from=333.999 icon-default.png?t=N3I4. 0.0            

If the content-type is application/x-www-form-urlencoded, which is in form format, use the data parameter

 Get interface request response data

In the process of using python interface testing, we often get a return value for a request interface. To make assertions or pass in other interfaces for use, let's look at several ways to get the returned results;


When the request interface requests, when the data is not processed, the returned data is requests.models.Response, and the return value cannot be obtained

At this point we need to process the returned data, as follows;
response.text # Response text data (string)

response.status_code # The status code of the response, which can be used as an interface assertion during testing
 

response.json() #The  returned data type becomes dict, which makes it convenient for us to get the data according to the operation of the dictionary

It is more convenient to obtain the response data through the dictionary method without conversion.

response.content # The content returned by the response (binary), generally used to obtain the returned data as files, pictures, or crawling videos

response.cookies # Get the returned cookies information

video tutorial

2023 latest collection of Python automated testing development framework [full stack/actual combat/tutorial] collection essence, annual salary 40W+ after learning _哔哩哔哩_bilibili https://www.bilibili.com/video/BV1AF411T7qJ/?spm_id_from=333.999 icon-default.png?t=N3I4. 0.0

 

 

Guess you like

Origin blog.csdn.net/MXB_1220/article/details/130494208