標籤:ima 參數錯誤 功能 int app file key color string
實現功能
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 sheetlist18 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 apiresult24 if rqtype == "post":25 apiresult = requests.post(url=rqurl, data=paramete, headers=headers)26 return apiresult27 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 continue47 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 continue53 self.compare_json_data(A.get(i), B.get(i), L, xpath + ‘/‘ + str(i))54 return55 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 L60 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 = 66612 # 擷取測試資料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_code24 assert code == 200, ‘▇▇▇請求失敗!‘+‘ ‘+‘code:‘+str(code)+‘ ‘+‘url=‘+CaseUrl25 pam = json.loads(apiresult.text)26 apitest.Assert(pam, Result)27 28 setattr(case, ‘__doc__‘, str(i[1]))29 return case30 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()
期間感謝@大師兄@三師兄@飯哥@無敵哥的指導
本人QQ:915069792
為什麼選擇?
有的人喜歡創造世界,他們做了程式員
有的人喜歡拯救世界,他們做了測試員
python+unittest+xlrd+request搭建API測試架構