python-requests資料驅動延伸 python-requests模組的講解和應用
阿新 • • 發佈:2018-12-07
在 python-requests模組的講解和應用 基礎上進行資料驅動的延伸
task_01_requests.py
#-*- coding:utf-8 -*- #task_01_requests.py # 1:利用requests模組,編寫一個可以完成http的get請求以及post請求的類。 # 2:利用登入和充值的兩個資料,完成1中編寫的類的單元測試(一條龍服務,包含測試報告) import requests class HttpRequests(): def http_requests(self,url,params,http_mothed,cookies=None):if http_mothed=='get': res=requests.get(url,params,cookies=cookies) return res else: res=requests.post(url,params,cookies=cookies) return res
task_02_requests.py
#-*- coding:utf-8 -*- #task_02_requests.py #存放測試用例 import unittest from study_181130_request.task_01_requests importHttpRequests COOKIES=None class TestHttpRequests(unittest.TestCase): # login_data={'mobilephone':18688773467,'pwd':'123456'} # login_data={'mobilephone':18688773467,'pwd':'1234567'} def setUp(self): # self.url='http://47.107.168.87:8080/futureloan/mvc/api/member/login' pass def__init__(self,url,param,http_method,excepted,methodName):#初始化傳參;用初始化函式一定要記得去看看父類裡面有沒有初始化函式,如果有,需要超繼承 self.param=param#請求的資料 self.http_method=http_method#請求方法 self.url=url self.excepted=excepted super(TestHttpRequests,self).__init__(methodName)#超繼承 # def test_login(self):#登入成功 :手機號正確、密碼正確;用例裡面不可以傳引數 def test_api(self):#登入成功 :手機號正確、密碼正確;用例裡面不可以傳引數 global COOKIES#宣告全域性變數 res=HttpRequests().http_requests(self.url,self.param,self.http_method,COOKIES) try: self.assertEqual(self.excepted,res.json()['msg']) except AssertionError as e: print('斷言結果是:{}'.format(e)) raise e if res.cookies: COOKIES=res.cookies#當res.cookies非空時,修改COOKIES的值 # def test_error_pwd(self):#登入成功 :手機號正確、密碼錯誤 # # res=HttpRequests().http_requests(self.url,self.param,self.http_method) # try: # self.assertEqual('使用者名稱或密碼錯誤',res.json()['msg']) # except AssertionError as e: # print('斷言結果是:{}'.format(e)) # raise e # # def test_recharge(self):#充值成功 # # recharge='http://47.107.168.87:8080/futureloan/mvc/api/member/recharge' # # recharge_data={'mobilephone':18688773467,'amount':'1000'} # expected='充值成功' # # res=HttpRequests().http_requests(recharge,recharge_data,'post',COOKIES) # actual=res.json()['msg'] # self.assertEqual(expected,actual) # 資料驅動延伸--思路整理: # 先把可以引數化的login放到setUp()函式裡,賦值給self.url # 再把請求資料login_data賦值給self.param和請求方法賦值給http_method放到初始化函式裡;初始化函式不能直接重寫,所以需要超繼承 # 引數化後要記得把函式裡的引數改為引數化後的引數 # task_03_requests.py中建立例項的方法來新增用例;然後裡面傳參 # 將login_data放到test_data列表中,然後利用for迴圈讀取列表中的值,使suit.addTest()程式碼量減少 # 然後再將放到test_data列表優化,加入http_method,列表裡面巢狀字典 # 新增充值的請求,此時url不能再放到setUp函式裡面,可以放到test_data列表裡面引數化 # test_api函式裡面需要傳入COOKIES引數,充值才能成功 # 斷言裡面的期望值進行引數化
task_03_requests.py
#-*- coding:utf-8 -*- #task_03_requests.py import unittest import HTMLTestRunnerNew from study_181130_request.task_02_requests import TestHttpRequests # login_data_01={'mobilephone':18688773467,'pwd':'123456'} # login_data_02={'mobilephone':18688773467,'pwd':'1234567'} # test_data=[{'mobilephone':18688773467,'pwd':'123456'},{'mobilephone':18688773467,'pwd':'1234567'}] test_data=[{'param':{'mobilephone':18688773467,'pwd':'123456'},'http_method':'get','excepted':'登入成功', 'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login'}, {'param':{'mobilephone':18688773467,'pwd':'1234567'},'http_method':'post','excepted':'使用者名稱或密碼錯誤', 'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/login'}, {'param':{'mobilephone':18688773467,'amount':'1000'},'http_method':'post','excepted':'充值成功', 'url':'http://47.107.168.87:8080/futureloan/mvc/api/member/recharge'}] suit=unittest.TestSuite()##裝用例的地方 for item in test_data: suit.addTest(TestHttpRequests(item['url'],item['param'],item['http_method'],item['excepted'],'test_api'))#建立例項的方法來新增用例 # suit.addTest(TestHttpRequests(item,'get','test_login'))#建立例項的方法來新增用例 # suit.addTest(TestHttpRequests(login_data_02,'post','test_login')) # loader=unittest.TestLoader() # suit.addTest(loader.loadTestsFromTestCase(TestHttpRequests))#通過loader從指定的測試類裡面新增用例 #執行用例 with open('test_api.html','bw+') as file: Runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file,verbosity=2, title='HTTP請求作業-單元測試報告',description='測試一下HTTP請求',tester='huimin' ) Runner.run(suit)
測試報告