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

發表于:2011-09-01來源:未知作者:領測軟件測試網采編點擊數: 標簽:自動化測試
一、測試需求描述 對服務后臺一系列的http接口功能測試。 輸入:根據接口描述構造不同的參數輸入值

  一、測試需求描述

  對服務后臺一系列的http接口功能測試。

  輸入:根據接口描述構造不同的參數輸入值

  輸出:XML文件

  eg:http://xxx.com/xxx_product/test/content_book_list.jsp?listid=1

  二、實現方法

  1、選用Python腳本來驅動測試

  2、采用Excel表格管理測試數據,包括用例的管理、測試數據錄入、測試結果顯示等等,這個需要封裝一個Excel的類即可。

  3、調用http接口采用Python封裝好的API即可

  4、測試需要的http組裝字符轉處理即可

  5、設置2個檢查點,XML文件中的返回值字段(通過解析XML得到);XML文件的正確性(文件對比)

  6、首次執行測試采用半自動化的方式,即人工檢查輸出的XML文件是否正確,一旦正確將封存XML文件,為后續回歸測試的預期結果,如果發現錯誤手工修正為預期文件。(注意不是每次測試都人工檢查該文件,只首次測試的時候才檢查)

  三、Excel表格樣式

  四、實現代碼(代碼才是王道,有注釋很容易就能看明白的)

  1、測試框架代碼

  view plaincopy to clipboardprint?

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

  # TestFrame.py

  # Author : Vince

  # Version : 1.1.2

  # Date : 2011-3-14

  # Description: 自動化測試平臺

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

  import os,sys, urllib, httplib, profile, datetime, time

  from xml2dict import XML2Dict

  import win32com.client

  from win32com.client import Dispatch

  import xml.etree.ElementTree as et

  #import MySQLdb

  #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 =3 #Excel中參數開始列索引

  #self.argcount =8 #Excel中支持的參數個數

  class create_excel:

  def __init__(self, sFile, dtitleindex=3, dcasebegin=4, dargbegin=3, dargcount=8):

  self.xlApp = win32com.client.Dispatch('et.Application') #MS:Excel WPS:et

  try:

  self.book = self.xlApp.Workbooks.Open(sFile)

  except:

  print_error_info()

  print "打開文件失敗"

  exit()

  self.file=sFile

  self.titleindex=dtitleindex

  self.casebegin=dcasebegin

  self.argbegin=dargbegin

  self.argcount=dargcount

  self.allresult=[]

  self.retCol=self.argbegin+self.argcount

  self.xmlCol=self.retCol+1

  self.resultCol=self.xmlCol+1

  def close(self):

  #self.book.Close(SaveChanges=0)

  self.book.Save()

  self.book.Close()

  #self.xlApp.Quit()

  del self.xlApp

  def 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 sValue

  def 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=color

  self.book.Save()

  except:

  self.close()

  print('寫入數據失敗')

  exit()

  #獲取用例個數

  def get_ncase(self, iSheet):

  try:

  return self.get_nrows(iSheet)-self.casebegin+1

  except:

  self.close()

  print('獲取Case個數失敗')

  exit()

  def get_nrows(self, iSheet):

  try:

  sht = self.book.Worksheets(iSheet)

  return sht.UsedRange.Rows.Count

  except:

  self.close()

  print('獲取nrows失敗')

  exit()

  def get_ncols(self, iSheet):

  try:

  sht = self.book.Worksheets(iSheet)

  return sht.UsedRange.Columns.Count

  except:

  self.close()

  print('獲取ncols失敗')

  exit()

  def del_testrecord(self, suiteid):

  try:

  #為提升性能特別從For循環提取出來

  nrows=self.get_nrows(suiteid)+1

  ncols=self.get_ncols(suiteid)+1

  begincol=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列中的測試結果,設置為NT

  self.write_data(suiteid, row, self.argbegin+self.argcount+1, ' ', OK_COLOR)

  self.write_data(suiteid, row, self.resultCol, 'NT', NT_COLOR)

  except:

  self.close()

  print('清除數據失敗')

  exit()

  #執行調用

  def HTTPInvoke(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))

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

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