Python interface test interface request method encapsulation

  introduction

  Three articles have been mentioned earlier:

1. Excel table data operation method encapsulation of Python interface test  ;

2. Interface keyword encapsulation for Python interface testing   ;

3. Get interface data package for Python interface test   ;

  Now that our interface test cases have been written and the test data has been obtained, it is a method of simulating the call to the interface. The methods are get, post, put, and delete. The specific choice is which. transfer. For convenience, the four methods are encapsulated into a class, each time only need to access a method in the class, to achieve the purpose of the request.

  Interface request encapsulation

  Attach the code directly below:

__author__ = 'Leo'

import requests
import json

class ApiRequest(object):
    """
    请求方法
    """
    # 请求方法get
    def get_method(self,url,data=None,header=None):

        if header is not None:
            res = requests.get(url,params=data,headers=header)
        else:
            res = requests.get(url,params=data)
        return res.json()

    # 请求方法post
    def post_method(self,url,data=None,header=None):
        global res
        if header is not None:
            res = requests.post(url,json=data,headers=header)
        else:
            res = requests.post(url,json=data)
        if  str(res) == "<Response [200]>":
            return res.json()
        else:
            return res.text

    # 请求方法put
    def put_method(self,url,data=None,header=None):
        if header is not None:
            res = requests.put(url,json=data,headers=header)
        else:
            res = requests.delete(url, json=data)
        return res.json()

    # 请求方法delete
    def delete_method(self, url, data=None, header=None):
        if header is not None:
            res = requests.delete(url, json=data, headers=header)
        else:
            res = requests.delete(url, json=data)
        return res.json()

    # 主方法
    def run_method(self,method,url,data=None,header=None):
        if method == 'get' or method == 'GET':
            res = self.get_method(url,data,header)
        elif method == 'post' or method =='POST':
            res = self.post_method(url,data,header)
        elif method == 'put' or method == 'PUT':
            res = self.post_method(url,data,header)
        elif method == 'delete' or method == 'DELETE':
            res = self.post_method(url,data,header)
        else:
            res = "你的请求方式不正确!"
        # return res
        return json.dumps(res, ensure_ascii=False, indent=4, sort_keys=True,separators=(',', ':'))

  

Briefly explain the relevant knowledge points:

ensure_ascii: The default value is True. If the dict contains non-ASCII characters, it will be similar to the display data of \ uXXXX. After setting it to False, it can be displayed normally. 

indent: It should be a non-negative integer type. If it is 0 or empty, the data will be displayed on one line, otherwise it will wrap and display the previous white space according to the number of indent. The json data printed out is also called pretty-printed json. 

separators: separators, actually a tuple of (item_separator, dict_separator), the default is (',', ':'); this means that the keys in the dictionary are separated by ",", and the KEY and value are The rooms are separated by ":". 

encoding: The default is UTF-8, which sets the encoding method of json data. 

sort_keys: Sort data according to the value of keys.

 

  Perform test

  The method has been encapsulated. Now that the interface data is available and the request method is available, we might as well test the previously encapsulated code and verify it with actual actions:

# get请求
if __name__ == '__main__':
    url = "http://httpbin.org/get"
    ir = ApiRequest()
    result = ir.run_method(url=url,method='get')
    print(result)

  operation result:

 

 

# post请求
url2 = "http://httpbin.org/post"
ir2 = ApiRequest()
data = {'id': '测试'}
result = ir2.run_method(url=url2,method='post',data=data)
print(result)

operation result:

 

 

  Some attentive people may have doubts. The interface test data in excel is not used here. Let me demonstrate below. Call the interface test data maintained in excel and maintain the data first:

 

  from basic_method import get_excelData 
    get_data = get_excelData.getData () 
    print ("Get whether to run key:", get_data.get_is_run (1)) 
    print ("Get interface url:", get_data.get_url (1)) 
    print ("Get interface request Method: ", get_data.get_method (1)) 
    print (" Get interface request data: ", get_data.get_data (1)) 
    url2 = get_data.get_url (1) 
    method = get_data.get_method (1) 
    data = get_data.get_data ( 1) 
    ir2 = ApiRequest () 
    result = ir2.run_method (url = url2, method = method, data = data) 
    print (result)

  

operation result:

 

 

  This time, the calling process takes the data from excel and requests the simulated request interface through the encapsulated method. Prove that the previously packaged code can run normally.

Here is another request method:

 

 

  to sum up

  The above interface request method packaging has been completed, and the previously written packaging code is used together to implement the core code in the automated testing framework. Friends who are interested in automated testing and test development can join the QQ test development exchange group. : 696400122. Learn from each other and make progress together!

Guess you like

Origin www.cnblogs.com/liudinglong/p/12755730.html