python3+requests:使用类封装接口测试脚本
程序员文章站
2023-12-31 08:17:46
前言:接口测试用例较多,我们不可能每个用例都写一次requests,get或者requests,post等,所以对共用方法要进行封装处理 第一次修改:将get请求和post请求单独定义出来,使用过程中根据不同类型的请求直接调用对应的方法 第二次修改:用class进行封装,主函数中创建一个实例run来 ......
前言:接口测试用例较多,我们不可能每个用例都写一次requests,get或者requests,post等,所以对共用方法要进行封装处理
第一次修改:将get请求和post请求单独定义出来,使用过程中根据不同类型的请求直接调用对应的方法
1 import requests 2 import json 3 4 def send_post(url,data,headers): 5 response = requests.post(url=url,data=data,headers=headers).json() 6 return json.dumps(response,sort_keys=True,indent=4) 7 8 def send_get(url,data,headers): 9 response = requests.get(url=url,data=data,headers=headers).json() 10 return json.dumps(response,sort_keys=True,indent=4) 11 12 def run_main(url,headers,method,data=None): 13 respose = None 14 if method == 'GET': 15 respose = send_get(url,data,headers) 16 else: 17 respose = send_post(url,data,headers) 18 return respose 19 20 url = 'https://...' 21 headers = {'Content-Type': 'application/x-www-form-urlencoded'} 22 print(run_main(url,headers,method='POST'))
第二次修改:用class进行封装,主函数中创建一个实例run来调用类中的方法
1 import requests 2 import json 3 class Runmain: 4 def send_post(self,url,data,headers): 5 response = requests.post(url=url,data=data,headers=headers).json() 6 return json.dumps(response,sort_keys=True,indent=4) 7 8 def send_get(self,url,params,headers): 9 response = requests.get(url=url,params=params,headers=headers).json() 10 return json.dumps(response,sort_keys=True,indent=4) 11 12 def run_main(self,url,params,data,headers,method): 13 respose = None 14 if method == 'GET': 15 respose = self.send_get(url,params,headers) 16 else: 17 respose = self.send_post(url,data,headers) 18 return respose 19 20 if __name__ == '__main__': 21 run = Runmain() 22 url = 'https://...' 23 headers = {'Content-Type': 'application/x-www-form-urlencoded'} 24 print(run.run_main(url,params=None,data=None,headers=headers,method='POST'))
第三次修改:第二次修改,每次都需要实例化后再调用对应的方法;
改进办法:使用__init__
方法实现:只要实例化类时候就会调用__init__方法
1 import requests 2 import json 3 class Runmain: 4 def __init__(self,url,params,data,headers,method): 5 self.response = self.run_main(url,params,data,headers,method) 6 7 def send_post(self,url,data,headers): 8 response = requests.post(url=url,data=data,headers=headers).json() 9 return json.dumps(response,sort_keys=True,indent=4) 10 11 def send_get(self,url,params,headers): 12 response = requests.get(url=url,params=params,headers=headers).json() 13 return json.dumps(response,sort_keys=True,indent=4) 14 15 def run_main(self,url,params,data,headers,method): 16 respose = None 17 if method == 'GET': 18 respose = self.send_get(url,params,headers) 19 else: 20 respose = self.send_post(url,data,headers) 21 return respose 22 23 if __name__ == '__main__': 24 url = 'https://...' 25 headers = {'Content-Type': 'application/x-www-form-urlencoded'} 26 run = Runmain(url,params=None,data=None,headers=headers,method='POST') 27 print(run.response)
备注:对python中类,实例不了解的同学,可以参考:
http://yangcongchufang.com/%E9%AB%98%E7%BA%A7python%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/python-object-class.html