實現Python Http 接口測試

實現Python Http 接口測試

一、測試需求對服務後臺一系列的http接口功能測試。
輸入:根據接口描述構造不同的參數輸入值輸出:XML文件或者JSON文件;
eg:http://XXX.com/XXX/test/video?name=movie 
二、實現方法
1、選用Python語言作爲測試腳本;
2、採用Excel表格管理測試數據,包括用例的管理、測試數據錄入、測試結果顯示等等,需要封裝一個Excel的類即可。
3、調用http接口採用Python封裝好的API即可
4、測試需要的http組裝字符轉處理即可
5、設置2個檢查點,XML文件或者JSON文件中的返回值字段(通過解析XML文件或者JSON文件得到);
XML文件或者JSON文件的正確性(對比文件)6、首次執行測試採用半自動化的方式,
即人工檢查輸出的XML文件或者JSON文件是否正確,一旦正確將封存XML文件或者JSON文件,爲後續迴歸測試的預期結果,如果發現錯誤手工修正爲預期文件。
注意:不是每次測試都人工檢查該文件,只首次測試的時候才檢查Excel表格爲,sheet名稱爲name




</pre><pre code_snippet_id="646266" snippet_file_name="blog_20150417_1_1728015" name="code" class="python"># -*- coding: utf-8 -*-
#!/usr/bin/python
'''
Created on 2015-4-10
@author: cyt
'''
import urllib, urllib2
import xlrd
import json


def GetTabName(tabname, method):
    if method=='GET':  #http方法
        url='http://XXX.com/XXX/test/content_book_list.jsp?listid='+listid
        result=urllib2.urlopen(url).read()
        return result
    if method == 'POST':
        url = 'http://ip:port/interface/GetUserInfo.php'
        values = {'uname' : tabname}
        data = urllib.urlencode(values)
        req = urllib2.Request(url, data)
        response = urllib2.urlopen(req)
        result = response.read()
        return result
   font-size:14px;">
def WriterLog():
    print
    
def GetWord():
    FileOpen=xlrd.open_workbook(r'D:\TestFile\video.xls') #測試腳本寫在xls中
    SheetOpen=FileOpen.sheet_by_name('name')
    nrows=SheetOpen.nrows
    for i in range(0, nrows):
        TestCase=SheetOpen.cell_value(i,0)
        tabname=SheetOpen.cell_value(i,1)
        method=SheetOpen.cell_value(i,2)
        ExpectedResult=SheetOpen.cell_value(i,3)
        #WriterLog('TestCase Name'+TestCase+'TestData:name='+name+',method='+method+'Excepted Result='+Excepted Result)
        print 'TestCase Name='+TestCase+' ,TestData:tname='+tname+' ,method='+method+' ,Excepted Result='+ExpectedResult
        Result1 = GetTabName(name,method) # 調用API接口
        #WriterLog('AC_result = ' + AC_result) # 寫測試日誌
        print Result1  # 寫測試日誌
        #print type(Result1)
        ResultFile=open(r'D:\TestFile\video.txt','w')
        print >> ResultFile,Result1
      

    
GetWord() # TestCase 腳本</span>






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章