1. 程式人生 > 實用技巧 >轉-python3+requests:使用類封裝介面測試指令碼

轉-python3+requests:使用類封裝介面測試指令碼

轉載請註明出處: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