u3d 模版测试 失败_基于Python的HTTP接口自动化测试框架实现
生活随笔
收集整理的這篇文章主要介紹了
u3d 模版测试 失败_基于Python的HTTP接口自动化测试框架实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、測試需求描述
對服務后臺一系列的http接口功能測試。
輸入:根據接口描述構造不同的參數輸入值
輸出:XML文件
二、實現方法
1、選用Python腳本來驅動測試
2、采用Excel表格管理測試數據,包括用例的管理、測試數據錄入、測試結果顯示等等,這個需要封裝一個Excel的類即可。
3、調用http接口采用Python封裝好的API即可
4、測試需要的http組裝字符轉處理即可
5、設置2個檢查點,XML文件中的返回值字段(通過解析XML得到);XML文件的正確性(文件對比)
6、首次執行測試采用半自動化的方式,即人工檢查輸出的XML文件是否正確,一旦正確將封存XML文件,為后續回歸測試的預期結果,如果發現錯誤手工修正為預期文件。(注意不是每次測試都人工檢查該文件,只首次測試的時候才檢查)
三、Excel表格樣式
四、實現代碼(代碼才是王道,有注釋很容易就能看明白的)
1、測試框架代碼
# -*- coding: utf-8 -*- #**************************************************************** # TestFrame.py # Author : Vince # Version : 1.1.2 # Date : 2011-3-14 # Description: # 平臺:自動化測試平臺 # 簡介:用于服務端自動化測試,該模塊為測試平臺主體,包含了執行自動化測試所需要的庫函數及相關框架,此外還包含了Excel標準模版。 # 使用約束:輸入數據必須符合Excel標準模版;測試腳本編寫遵循自動化測試框架規范(具體參見樣例) # # 功能實現列表: # 1、Excel操作封裝 # 2、參數組裝 # 3、測試調用 # 4、測試報告保存 # # 適用范圍: # 1、典型輸入輸出測試接口的測試,輸入輸出為一具體值,輸入可以為多個參數,輸出暫時支持1個輸出,后期可考慮支持多輸出的判斷; # 2、輸出返回XML文件,解析XML判斷測試結果; # 3、輸出為一個壓縮包,解壓包判斷測試結果; # 4、...... # # 版本修改歷史 # 版本號 修改人 修改時間 修改說明 # 1.0.0 Vince 2011-2-18 創建 # 1.1.0 Vince 2011-3-1 修改操作EXCEL類 create_excel_obj # 1.1.1 Vince 2011-3-8 增加解析XML函數 # 1.1.2 Vince 2011-3-14 統一注釋 # 1.2.0 Vince 2011-3-18 修改了Excel模版樣式,增加了返回值中同時檢查多個Item及結構體的函數 # 1.3.0 Vince 2011-3-19 Excel對象操作由原來使用xlrd改用win32com,修改了create_excel類 # 1.4.0 Vince 2011-3-20 增強Excel顯示的可讀性;增加用例執行控制;增加用例執行統計功能;改善測試框架性能(提升1.5倍) # 1.5.0 Vince 2011-3-22 將Win32com訪問Excel表格由MS Excel改為WPS ET # 1.5.1 Vince 2011-3-28 check_struct_item中調用check_item函數修改 #****************************************************************import os,sys, urllib, httplib, profile, datetime, time from xml2dict import XML2Dict import win32com.client from win32com.client import Dispatch#Excel表格中測試結果底色 OK_COLOR=0xffffff NG_COLOR=0xff #NT_COLOR=0xffff NT_COLOR=0xC0C0C0#Excel表格中測試結果匯總顯示位置 TESTTIME=[1, 14] TESTRESULT=[2, 14]#Excel模版設置 #self.titleindex=3 #Excel中測試用例標題行索引 #self.casebegin =4 #Excel中測試用例開始行索引 #self.argbegin =2 #Excel中參數開始列索引 #self.argcount =5 #Excel中支持的參數個數 #self.resultcol =28 #TestResult列 class create_excel:def __init__(self, sFile, dtitleindex=3, dcasebegin=4, dargbegin=3, dargcount=5, dresultcol=28):self.xlApp = win32com.client.Dispatch('et.Application') #MS:Excel WPS:ettry:self.book = self.xlApp.Workbooks.Open(sFile)except:print_error_info()print "打開文件失敗"exit()self.file=sFileself.titleindex=dtitleindexself.casebegin=dcasebeginself.argbegin=dargbeginself.argcount=dargcountself.resultcol=dresultcolself.allresult=[]def close(self):#self.book.Close(SaveChanges=0)self.book.Save()self.book.Close()#self.xlApp.Quit()del self.xlAppdef read_data(self, iSheet, iRow, iCol):try:sht = self.book.Worksheets(iSheet)sValue=str(sht.Cells(iRow, iCol).Value)except:self.close()print('讀取數據失敗')exit()#去除'.0'if sValue[-2:]=='.0':sValue = sValue[0:-2]return sValuedef write_data(self, iSheet, iRow, iCol, sData, color=OK_COLOR):try:sht = self.book.Worksheets(iSheet)sht.Cells(iRow, iCol).Value = sData.decode("utf-8")sht.Cells(iRow, iCol).Interior.Color=colorself.book.Save()except:self.close()print('寫入數據失敗')exit()#獲取用例個數 def get_ncase(self, iSheet):try:return self.get_nrows(iSheet)-self.casebegin+1except:self.close()print('獲取Case個數失敗')exit()def get_nrows(self, iSheet):try:sht = self.book.Worksheets(iSheet)return sht.UsedRange.Rows.Countexcept:self.close()print('獲取nrows失敗')exit()def get_ncols(self, iSheet):try:sht = self.book.Worksheets(iSheet)return sht.UsedRange.Columns.Countexcept:self.close()print('獲取ncols失敗')exit()def del_testrecord(self, suiteid):try:#為提升性能特別從For循環提取出來nrows=self.get_nrows(suiteid)+1ncols=self.get_ncols(suiteid)+1begincol=self.argbegin+self.argcount#提升性能sht = self.book.Worksheets(suiteid)for row in range(self.casebegin, nrows):for col in range(begincol, ncols):str=self.read_data(suiteid, row, col)#清除實際結果[]startpos = str.find('[')if startpos>0:str = str[0:startpos].strip()self.write_data(suiteid, row, col, str, OK_COLOR)else:#提升性能sht.Cells(row, col).Interior.Color = OK_COLOR#清除TestResul列中的測試結果,設置為NTself.write_data(suiteid, row, self.resultcol, 'NT', NT_COLOR)except:self.close()print('清除數據失敗')exit()#執行調用 def run_case(IPPort, url):conn = httplib.HTTPConnection(IPPort)conn.request("GET", url)rsps = conn.getresponse()data = rsps.read()conn.close()return data#獲取用例基本信息[Interface,argcount,[ArgNameList]] def get_caseinfo(Data, SuiteID):caseinfolist=[]sInterface=Data.read_data(SuiteID, 1, 2) argcount=int(Data.read_data(SuiteID, 2, 2)) #獲取參數名存入ArgNameList ArgNameList=[]for i in range(0, argcount):ArgNameList.append(Data.read_data(SuiteID, Data.titleindex, Data.argbegin+i)) caseinfolist.append(sInterface)caseinfolist.append(argcount)caseinfolist.append(ArgNameList)return caseinfolist#獲取輸入 def get_input(Data, SuiteID, CaseID, caseinfolist):sArge=''#參數組合for j in range(0, caseinfolist[1]):sArge=sArge+caseinfolist[2][j]+'='+Data.read_data(SuiteID, Data.casebegin+CaseID, Data.argbegin+j)+'&' #去掉結尾的&字符if sArge[-1:]=='&':sArge = sArge[0:-1] sInput=caseinfolist[0]+sArge #組合全部參數return sInput#結果判斷 def assert_result(sReal, sExpect):sReal=str(sReal)sExpect=str(sExpect)if sReal==sExpect:return 'OK'else:return 'NG'#將測試結果寫入文件 def write_result(Data, SuiteId, CaseId, resultcol, *result):if len(result)>1:ret='OK'for i in range(0, len(result)):if result[i]=='NG':ret='NG'breakif ret=='NG':Data.write_data(SuiteId, Data.casebegin+CaseId, resultcol,ret, NG_COLOR)else:Data.write_data(SuiteId, Data.casebegin+CaseId, resultcol,ret, OK_COLOR)Data.allresult.append(ret)else:if result[0]=='NG':Data.write_data(SuiteId, Data.casebegin+CaseId, resultcol,result[0], NG_COLOR)elif result[0]=='OK':Data.write_data(SuiteId, Data.casebegin+CaseId, resultcol,result[0], OK_COLOR)else: #NTData.write_data(SuiteId, Data.casebegin+CaseId, resultcol,result[0], NT_COLOR)Data.allresult.append(result[0])#將當前結果立即打印print 'case'+str(CaseId+1)+':', Data.allresult[-1]#打印測試結果 def statisticresult(excelobj):allresultlist=excelobj.allresultcount=[0, 0, 0]for i in range(0, len(allresultlist)):#print 'case'+str(i+1)+':', allresultlist[i]count=countflag(allresultlist[i],count[0], count[1], count[2])print 'Statistic result as follow:'print 'OK:', count[0]print 'NG:', count[1]print 'NT:', count[2]#解析XmlString返回Dict def get_xmlstring_dict(xml_string):xml = XML2Dict()return xml.fromstring(xml_string)#解析XmlFile返回Dict def get_xmlfile_dict(xml_file):xml = XML2Dict()return xml.parse(xml_file)#去除歷史數據expect[real] def delcomment(excelobj, suiteid, iRow, iCol, str):startpos = str.find('[')if startpos>0:str = str[0:startpos].strip()excelobj.write_data(suiteid, iRow, iCol, str, OK_COLOR)return str#檢查每個item (非結構體) def check_item(excelobj, suiteid, caseid,real_dict, checklist, begincol):ret='OK'for checkid in range(0, len(checklist)):real=real_dict[checklist[checkid]]['value']expect=excelobj.read_data(suiteid, excelobj.casebegin+caseid, begincol+checkid)#如果檢查不一致測將實際結果寫入expect字段,格式:expect[real]#將return NGresult=assert_result(real, expect)if result=='NG':writestr=expect+'['+real+']'excelobj.write_data(suiteid, excelobj.casebegin+caseid, begincol+checkid, writestr, NG_COLOR)ret='NG'return ret#檢查結構體類型 def check_struct_item(excelobj, suiteid, caseid,real_struct_dict, structlist, structbegin, structcount):ret='OK'if structcount>1: #傳入的是Listfor structid in range(0, structcount):structdict=real_struct_dict[structid]temp=check_item(excelobj, suiteid, caseid,structdict, structlist, structbegin+structid*len(structlist))if temp=='NG':ret='NG'else: #傳入的是Dicttemp=check_item(excelobj, suiteid, caseid,real_struct_dict, structlist, structbegin)if temp=='NG':ret='NG'return ret#獲取異常函數及行號 def print_error_info():"""Return the frame object for the caller's stack frame."""try:raise Exceptionexcept:f = sys.exc_info()[2].tb_frame.f_backprint (f.f_code.co_name, f.f_lineno) #測試結果計數器,類似Switch語句實現 def countflag(flag,ok, ng, nt): calculation = {'OK':lambda:[ok+1, ng, nt], 'NG':lambda:[ok, ng+1, nt], 'NT':lambda:[ok, ng, nt+1]} return calculation[flag]()2、項目測試代碼
# -*- coding: utf-8 -*- #**************************************************************** # module1_case.py # Author : Vince # Version : 1.0 # Date : 2011-3-10 # Description: 內容服務系統測試代碼 #**************************************************************** from testframe import *STRUCTCOL=14 #結構體開始列#content_book_list用例集 def content_book_list_suite():print 'Test Begin,please waiting...'suiteid=4 #設置測試輸入Sheetindex=1excelobj.del_testrecord(suiteid) #清除歷史測試數據casecount=excelobj.get_ncase(suiteid)checklist=['resultCode', 'listid', 'name', 'total', 'from', 'size']structitem=['bookid', 'name', 'title', 'progress', 'words', 'authorid', 'authorName']#獲取Case基本信息caseinfolist=get_caseinfo(excelobj, suiteid)for caseid in range(0, casecount):#檢查是否執行該Caseif excelobj.read_data(suiteid,excelobj.casebegin+caseid, 2)=='N':write_result(excelobj, suiteid, caseid, excelobj.resultcol, 'NT')continue #當前Case結束,繼續執行下一個Case#獲取測試數據sInput=get_input(excelobj, suiteid, caseid, caseinfolist) XmlString=run_case(com_ipport, sInput) #執行調用dict=get_xmlfile_dict(os.getcwd()+'docTest.xml')itemdict=dict['root']ret1=check_item(excelobj, suiteid, caseid,itemdict, checklist, excelobj.argbegin+excelobj.argcount)structdict=dict['root']['books']['book']ret2=check_struct_item(excelobj, suiteid, caseid,structdict, structitem, STRUCTCOL, 2)write_result(excelobj, suiteid, caseid, excelobj.resultcol, ret1, ret2)print 'Test End!'3、測試入口
# -*- coding: utf-8 -*- #**************************************************************** # main.py # Author : Vince # Version : 1.0 # Date : 2011-3-16 # Description: 測試組裝,用例執行入口 #**************************************************************** from testframe import * from xxx_server_case import * from xxx_server_case import * import xxx_server_case#服務系統接口測試 #設置測試環境 xxx_server_case.excelobj=create_excel(os.getcwd()+'docTestDemo_testcase.xls') xxx_server_case.com_ipport='XXX.16.1.35:8080'begin =datetime.datetime.now() #Add testsuite begin content_book_list_suite() #Add other suite from here #Add testsuite end #profile.run("content_book_list_suite()")print 'Total Time:', datetime.datetime.now()-begin statisticresult(xxx_server_case.excelobj) xxx_server_case.excelobj.close()點贊關注~~領取技術資料
總結
以上是生活随笔為你收集整理的u3d 模版测试 失败_基于Python的HTTP接口自动化测试框架实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stm32延时us寄存器_STM32延时
- 下一篇: websocket python爬虫_p