python+unittest+xlrd+request搭建API測試框架
阿新 • • 發佈:2017-07-29
類型 string sts 所有 assert 測試結果 workbook .text ict
實現功能
1.可在表格中進行編寫用例
2.自動執行表格中測試用例
3.對響應結果進行深度斷言,可定位預期結果與測試結果的不同值與位置
4.形成HTML格式的測試報告
源碼可在githube中下載: https://github.com/wcnszbd/Mytest
先附上效果圖:
再上代碼: 結構圖
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2017-07-28 18:07 4 import xlrd 5 import requests 6 class APiTool: 7 8 #調取表格中用例方法 9 def xlsee(self, xlsFile): 10 sheetlist = [] # 用來保存表格所有數據 11 rqapi = xlrd.open_workbook(xlsFile) # 獲得文件對象 12 sheet_name = rqapi.sheet_names()[0] # 獲取第一個sheet名稱 13 sheet = rqapi.sheet_by_name(sheet_name) # 獲取第一個sheet對象 14 nrow = sheet.nrows #獲取行總數 15 for i in range(1,nrow): 16 sheetlist.append(sheet.row_values(i)) 17 return sheetlist 18 19 # 請求方法 20 def request(self, rqtype, rqurl, paramete, headers): 21 if rqtype == "get": 22 apiresult = requests.get(url=rqurl, params=paramete, headers=headers) #發送請求 23 return apiresult 24 if rqtype == "post": 25 apiresult = requests.post(url=rqurl, data=paramete, headers=headers) 26 return apiresult 27 else: 28 print("請求參數錯誤,請求類型只支持get+post,請求地址支持string,參數支持dict") 29 30 31 # 對返回的json值進行深度斷言 32 33 def compare_json_data(self,A, B, L = [], xpath = ‘.‘): 34 if isinstance(A, list) and isinstance(B, list): 35 for i in range(len(A)): 36 try: 37 self.compare_json_data(A[i], B[i], L, xpath + ‘[%s]‘ % str(i)) 38 except: 39 L.append(‘▇▇▇ A中的key %s[%s]未在B中找到\n‘ % (xpath, i)) 40 if isinstance(A, dict) and isinstance(B, dict): 41 for i in A: 42 try: 43 B[i] 44 except: 45 L.append(‘▇▇▇ A中的key %s/%s 未在B中找到\n‘ % (xpath, i)) 46 continue 47 if not (isinstance(A.get(i), (list, dict)) or isinstance(B.get(i), (list, dict))): 48 if type(A.get(i)) != type(B.get(i)): 49 L.append(‘▇▇▇ 類型不同參數在[A]中的絕對路徑: %s/%s ??? A is %s, B is %s \n‘ % (xpath, i, type(A.get(i)), type(B.get(i)))) 50 elif A.get(i) != B.get(i): 51 L.append(‘▇▇▇ 僅內容不同參數在[A]中的絕對路徑: %s/%s ??? A is %s, B is %s \n‘ % (xpath, i, A.get(i), B.get(i))) 52 continue 53 self.compare_json_data(A.get(i), B.get(i), L, xpath + ‘/‘ + str(i)) 54 return 55 if type(A) != type(B): 56 L.append(‘▇▇▇ 類型不同參數在[A]中的絕對路徑: %s ??? A is %s, B is %s \n‘ % (xpath, type(A), type(B))) 57 elif A != B and type(A) is not list: 58 L.append(‘▇▇▇ 僅內容不同參數在[A]中的絕對路徑: %s ??? A is %s, B is %s \n‘ % (xpath, A, B)) 59 return L 60 61 def Assert(self,A,B): 62 C = [] 63 self.compare_json_data(A, B, C) 64 assert len(C) == 0, "\n"+"".join(C)
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2017-07-28 18:07 4 import unittest 5 import HTMLTestRunner 6 import time 7 import tool 8 import json 9 10 class Test(unittest.TestCase): 11 heixiongjing = 666 12 # 獲取測試數據 13 14 # 閉包函數用於生成用例 15 16 def demo(i): 17 def case(self): 18 CaseUrl = i[2]+i[3] 19 RequestType = i[4] 20 Paramete = i[5] 21 Result = json.loads(i[6]) 22 apiresult = apitest.request(RequestType, CaseUrl, Paramete, ‘‘) 23 code = apiresult.status_code 24 assert code == 200, ‘▇▇▇請求失敗!‘+‘ ‘+‘code:‘+str(code)+‘ ‘+‘url=‘+CaseUrl 25 pam = json.loads(apiresult.text) 26 apitest.Assert(pam, Result) 27 28 setattr(case, ‘__doc__‘, str(i[1])) 29 return case 30 31 # 根據用例條數循環生成用例 32 def testall(num): 33 for i in num: 34 setattr(Test, ‘test_‘+str(int(i[0])), demo(i)) 35 36 37 if __name__ == "__main__": 38 apitest = tool.APiTool() 39 xlsFile = r"D:\myapi_test2\apicase.xls" # 文件路徑 40 sheetlist1 = apitest.xlsee(xlsFile) 41 testall(sheetlist1) 42 suit = unittest.makeSuite(Test) 43 now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime(time.time())) 44 filename = "D:\\myapi_test2\\report\\"+now+‘result.html‘ #定義個報告存放路徑,支持相對路徑。 45 fp = open(filename, ‘wb‘) 46 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=‘自動化測試報告‘, description=‘XX平臺V1.0‘) 47 runner.run(suit) 48 # unittest.main()
[email protected]@[email protected]@無敵哥的指導
本人QQ:915069792
為什麽選擇?
有的人喜歡創造世界,他們做了程序員
有的人喜歡拯救世界,他們做了測試員
python+unittest+xlrd+request搭建API測試框架