• 軟件測試技術
  • 軟件測試博客
  • 軟件測試視頻
  • 開源軟件測試技術
  • 軟件測試論壇
  • 軟件測試沙龍
  • 軟件測試資料下載
  • 軟件測試雜志
  • 軟件測試人才招聘
    暫時沒有公告

字號: | 推薦給好友 上一篇 | 下一篇

如何通過http接口實現自動化測試框架

發布: 2011-4-22 19:56 | 作者: 張元禮 | 來源: 領測軟件測試網采編 | 查看: 93次 | 進入軟件測試論壇討論

領測軟件測試網

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))   
      
    #獲取參數名存入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]):  
        if Data.read_data(SuiteID, Data.casebegin+CaseID, Data.argbegin+j) != "None":  
            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' 
                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]()  
#****************************************************************
# 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))
   
    #獲取參數名存入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]):
        if Data.read_data(SuiteID, Data.casebegin+CaseID, Data.argbegin+j) != "None":
            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'
                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!' 
# -*- 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!'

3、測試入口

view plaincopy to clipboardprint?
# -*- coding: utf-8 -*-  
#****************************************************************  
# main.py  
# Author     : Vince  
# Version    : 1.0  
# Date       : 2011-3-16  
# Description: 測試組裝,用例執行入口  
#****************************************************************  
 
from testframe import *  
from xxx_server_case import *  
import xxx_server_case  
 
#產品系統接口測試  
#設置測試環境  
xxx_server_case.excelobj=create_excel(os.getcwd()+'\TestDir\xxx_Testcase.xls')  
xxx_server_case.com_ipport=xxx.com'  
 
#Add testsuite begin  
run("xxx_book_list", 4)  
#Add other suite from here  
#Add testsuite end  
 
statisticresult(xxx_server_case.excelobj)  
xxx_server_case.excelobj.close() 
# -*- coding: utf-8 -*-
#****************************************************************
# main.py
# Author     : Vince
# Version    : 1.0
# Date       : 2011-3-16
# Description: 測試組裝,用例執行入口
#****************************************************************

from testframe import *
from xxx_server_case import *
import xxx_server_case

#產品系統接口測試
#設置測試環境
xxx_server_case.excelobj=create_excel(os.getcwd()+'\TestDir\xxx_Testcase.xls')
xxx_server_case.com_ipport=xxx.com'

#Add testsuite begin
run("xxx_book_list", 4)
#Add other suite from here
#Add testsuite end

statisticresult(xxx_server_case.excelobj)
xxx_server_case.excelobj.close()


最后感謝我的同事Roger為此做了一些優化,后續優化的東東還很多,我們一直在努力!

歡迎轉載此文,轉載時請注明文章來源:張元禮的博客 http://blog.csdn.net/vincetest

延伸閱讀

文章來源于領測軟件測試網 http://www.anti-gravitydesign.com/

TAG: Excel excel 測試框架 自動化


關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

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