http接口自動化測試框架實現(3)

發表于:2011-09-01來源:未知作者:領測軟件測試網采編點擊數: 標簽:自動化測試
#將測試結果寫入文件 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 break if ret==NG: Data.write_

  #將測試結果寫入文件

  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'

  break

  if 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: #NT

  Data.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.allresult

  count=[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 NG

  result=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: #傳入的是List

  for 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: #傳入的是Dict

  temp=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 Exception

  except:

  f = sys.exc_info()[2].tb_frame.f_back

  print (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、項目測試代碼

  view plaincopy to clipboardprint?

  # -*- coding: utf-8 -*-

  #****************************************************************

  # xxx_server_case.py

  # Author : Vince

  # Version : 1.0

  # Date : 2011-3-10

  # Description: 內容服務系統測試代碼

  #****************************************************************

  from testframe import *

  from common_lib import *

  httpString='http://xxx.com/xxx_product/test/'

  expectXmldir=os.getcwd()+'/TestDir/expect/'

  realXmldir=os.getcwd()+'/TestDir/real/'

  def run(interface_name, suiteid):

  print '【'+interface_name+'】' + ' Test Begin,please waiting...'

  global expectXmldir, realXmldir

  #根據接口名分別創建預期結果目錄和實際結果目錄

  expectDir=expectXmldir+interface_name

  realDir=realXmldir+interface_name

  if os.path.exists(expectDir) == 0:

  os.makedirs(expectDir)

  if os.path.exists(realDir) == 0:

  os.makedirs(realDir)

  excelobj.del_testrecord(suiteid) #清除歷史測試數據

  casecount=excelobj.get_ncase(suiteid) #獲取case個數

  caseinfolist=get_caseinfo(excelobj, suiteid) #獲取Case基本信息

  #遍歷執行case

  for caseid in range(0, casecount):

  #檢查是否執行該Case

  if excelobj.read_data(suiteid,excelobj.casebegin+caseid, 2)=='N':

  write_result(excelobj, suiteid, caseid, excelobj.resultCol, 'NT')

  continue #當前Case結束,繼續執行下一個Case

  #獲取測試數據

  sInput=httpString+get_input(excelobj, suiteid, caseid, caseinfolist)

  XmlString=HTTPInvoke(com_ipport, sInput) #執行調用

  #獲取返回碼并比較

  result_code=et.fromstring(XmlString).find("result_code").text

  ret1=check_result(excelobj, suiteid, caseid,result_code, excelobj.retCol)

  #保存預期結果文件

  expectPath=expectDir+'/'+str(caseid+1)+'.xml'

  #saveXmlfile(expectPath, XmlString)

  #保存實際結果文件

  realPath=realDir+'/'+str(caseid+1)+'.xml'

  saveXmlfile(realPath, XmlString)

  #比較預期結果和實際結果

  ret2= check_xmlfile(excelobj, suiteid, caseid,expectPath, realPath)

  #寫測試結果

  write_result(excelobj, suiteid, caseid, excelobj.resultCol, ret1, ret2)

  print '【'+interface_name+'】' + ' Test End!'

  

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97