http接口自动化测试框架实现作者:张元礼一、测试需求描述对服务后台一系列的http接口功能测试。输入:根据接口描述构造不同的参数输入值输出:XML文件eg:=1二、实现方法1、选用Python脚本来驱动测试2、采用Excel表格管理测试数据,包括用例的管理、测试数据录入、测试结果显示等等,这个需要封装一个Excel的类即可。3、调用http接口采用Python封装好的API即可4、测试需要的http组装字符转处理即可5、设置2个检查点,XML文件中的返回值字段(通过解析XML得到);XML文件的正确性(文件对比)6、首次执行测试采用半自动化的方式,即人工检查输出的XML文件是否正确,一旦正确将封存XML文件,为后续回归测试的预期结果,如果发现错误手工修正为预期文件。(注意不是每次测试都人工检查该文件,只首次测试的时候才检查)三、Excel表格样式四、实现代码(代码才是王道,有注释很容易就能看明白的)1、测试框架代码[python]viewplaincopy1.#****************************************************************2.#TestFrame.py3.#Author:Vince4.#Version:1.1.25.#Date:2011-3-146.#Description:自动化测试平台7.#****************************************************************8.9.importos,sys,urllib,httplib,profile,datetime,time10.fromxml2dictimportXML2Dict11.importwin32com.client12.fromwin32com.clientimportDispatch13.importxml.etree.ElementTreeaset14.#importMySQLdb15.16.#Excel表格中测试结果底色17.OK_COLOR=0xffffff18.NG_COLOR=0xff19.#NT_COLOR=0xffff20.NT_COLOR=0xC0C0C021.22.#Excel表格中测试结果汇总显示位置23.TESTTIME=[1,14]24.TESTRESULT=[2,14]25.26.#Excel模版设置27.#self.titleindex=3#Excel中测试用例标题行索引28.#self.casebegin=4#Excel中测试用例开始行索引29.#self.argbegin=3#Excel中参数开始列索引30.#self.argcount=8#Excel中支持的参数个数31.classcreate_excel:32.def__init__(self,sFile,dtitleindex=3,dcasebegin=4,dargbegin=3,dargcount=8):33.self.xlApp=win32com.client.Dispatch('et.Application')#MS:ExcelWPS:et34.try:35.self.book=self.xlApp.Workbooks.Open(sFile)36.except:37.print_error_info()38.print打开文件失败39.exit()40.self.file=sFile41.self.titleindex=dtitleindex42.self.casebegin=dcasebegin43.self.argbegin=dargbegin44.self.argcount=dargcount45.self.allresult=[]46.47.self.retCol=self.argbegin+self.argcount48.self.xmlCol=self.retCol+149.self.resultCol=self.xmlCol+150.51.defclose(self):52.#self.book.Close(SaveChanges=0)53.self.book.Save()54.self.book.Close()55.#self.xlApp.Quit()56.delself.xlApp57.58.defread_data(self,iSheet,iRow,iCol):59.try:60.sht=self.book.Worksheets(iSheet)61.sValue=str(sht.Cells(iRow,iCol).Value)62.except:63.self.close()64.print('读取数据失败')65.exit()66.#去除'.0'67.ifsValue[-2:]=='.0':68.sValue=sValue[0:-2]69.returnsValue70.71.defwrite_data(self,iSheet,iRow,iCol,sData,color=OK_COLOR):72.try:73.sht=self.book.Worksheets(iSheet)74.sht.Cells(iRow,iCol).Value=sData.decode(utf-8)75.sht.Cells(iRow,iCol).Interior.Color=color76.self.book.Save()77.except:78.self.close()79.print('写入数据失败')80.exit()81.82.#获取用例个数83.defget_ncase(self,iSheet):84.try:85.returnself.get_nrows(iSheet)-self.casebegin+186.except:87.self.close()88.print('获取Case个数失败')89.exit()90.91.defget_nrows(self,iSheet):92.try:93.sht=self.book.Worksheets(iSheet)94.returnsht.UsedRange.Rows.Count95.except:96.self.close()97.print('获取nrows失败')98.exit()99.100.defget_ncols(self,iSheet):101.try:102.sht=self.book.Worksheets(iSheet)103.returnsht.UsedRange.Columns.Count104.except:105.self.close()106.print('获取ncols失败')107.exit()108.109.defdel_testrecord(self,suiteid):110.try:111.#为提升性能特别从For循环提取出来112.nrows=self.get_nrows(suiteid)+1113.ncols=self.get_ncols(suiteid)+1114.begincol=self.argbegin+self.argcount115.116.#提升性能117.sht=self.book.Worksheets(suiteid)118.119.forrowinrange(self.casebegin,nrows):120.forcolinrange(begincol,ncols):121.str=self.read_data(suiteid,row,col)122.#清除实际结果[]123.startpos=str.find('[')124.ifstartpos0:125.str=str[0:startpos].strip()126.self.write_data(suiteid,row,col,str,OK_COLOR)127.else:128.#提升性能129.sht.Cells(row,col).Interior.Color=OK_COLOR130.#清除TestResul列中的测试结果,设置为NT131.self.write_data(suiteid,row,self.argbegin+self.argcount+1,'',OK_COLOR)132.self.write_data(suiteid,row,self.resultCol,'NT',NT_COLOR)133.except:134.self.close()135.print('清除数据失败')136.exit()137.138.#执行调用139.defHTTPInvoke(IPPort,url):140.conn=httplib.HTTPConnection(IPPort)141.conn.request(GET,url)142.rsps=conn.getresponse()143.data=rsps.read()144.conn.close()145.returndata146.147.#获取用例基本信息[Interface,argcount,[ArgNameList]]148.defget_caseinfo(Data,SuiteID):149.caseinfolist=[]150.sInterface=Data.read_data(SuiteID,1,2)151.argcount=int(Data.read_data(SuiteID,2,2))152.153.#获取参数名存入ArgNameList154.ArgNameList=[]155.foriinrange(0,argcount):156.ArgNameList.append(Data.read_data(SuiteID,Data.titleindex,Data.argbegin+i))157.158.caseinfolist.append(sInterface)159.caseinfolist.append(argcount)160.caseinfolist.append(ArgNameList)161.returncaseinfolist162.163.#获取输入164.defget_input(Data,SuiteID,CaseID,caseinfolist):165.sArge=''166.#参数组合167.forjinrange(0,caseinfolist[1]):168.ifData.read_data(SuiteID,Data.casebegin+CaseID,Data.argbegin+j)!=None:169.sArge=sArge+caseinfolist[2][j]+'='+Data.read_data(SuiteID,Data.casebegin+CaseID,Data.argbegin+j)+'&'170.171.#去掉结尾的&字符172.ifsArge[-1:]=='&':173.sArge=sArge[0:-1]174.sInput=caseinfolist[0]+sArge#组合全部参数175.returnsInput176.177.#结果判断178.defassert_result(sReal,sExpect):179.sReal=str(sReal)180.sExpect=str(sExpect)181.ifsReal==sExpect:182.return'OK'183.else:184.return'NG'185.186.#将测试结果写入文件187.defwrite_result(Data,SuiteId,CaseId,resultcol,*result):188.iflen(result)1: