我思故我在我有我精彩--liangqihui

爱欲追而情已逝,子欲孝而亲不待。人生的困苦又怎能用一个难字囊尽百味
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

转-python3+requests:使用类封装接口测试脚本

Posted on 2020-09-18 22:07  挥辉  阅读(295)  评论(0编辑  收藏  举报

转载请注明出处:https://www.cnblogs.com/shapeL/p/9045439.html

前言:接口测试用例较多,我们不可能每个用例都写一次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

 

 

https://blog.csdn.net/liguilicsdn/article/details/101062594?utm_medium=distribute.pc_relevant.none-task-blog-title-6&spm=1001.2101.3001.4242