python 接口自動化測試(四)


說完了SOAP協議的接口自動化

該說下http協議的接口測試了

HttpService.py

import requests
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )



class HttpService():
    def __init__(self,host,url):
        self.host=host
        self.url=url

    def httpReq(self,params,method="POST"):
        requesturl=self.host+self.url
        if(method == "POST"):
            resp_data=requests.post(requesturl,params)
        else:
            resp_data=requests.get(requesturl)
        resp=resp_data.text
        return resp
View Code

這是http請求的實現類,運用了requests模塊

之后就是在具體的執行文件中調用上面的HttpService.py

在RunMain.py中添加方法:

def runHttpTest(ip,list,serviceClass):
    length=len(list)
    #實現控制台輸出執行進度
    widgets = [ip+' Progress: ', Percentage(), ' ', Bar(marker=RotatingMarker('>')),
      ' ', ETA()]
    pbar = ProgressBar(widgets=widgets, maxval=length).start()
    for i in range(length):
        pbar.update(i+1)
        try:
            #實例化類的對象
            runhttptest = serviceClass(ip,str(list[i][0]))
            if(str(list[i][2]) == "GET"): #判斷http請求的方法
                test_rep=runhttptest.httpReq(None,"GET")
                DataEngine.httpGetResultCheck(test_rep,xlw,list,i) #返回結果判斷
            else:
                dict=eval(str(list[i][2]))
                test_rep=runhttptest.httpReq(dict)
                DataEngine.httpResultCheck(test_rep,xlw,list,i)

        except Exception,e:
            print(str(list[i][1])+"\t"+str(e))
            Logging.writeException(e)
    pbar.finish()
View Code

由於接口返回結果和SOAP接口的返回結果類型不一致,所以DataEngine.py中的結果處理方法就要做一些擴展,由於時間關系,我這邊就直接另寫了兩個方法進行結果斷言處理

def httpResultCheck(test_rep,xlw,list,xls_row):
    global COUNT_ROWs
    result=test_rep.find(',"status":1',22)
    if(result != -1):
        xlw.write_HttpResult(COUNT_ROWs,list,xls_row,True)
        COUNT_ROWs=COUNT_ROWs+1
    else:
        Logging.writeHttpLog(str(list[xls_row][1]),str(test_rep))
        xlw.write_HttpResult(COUNT_ROWs,list,xls_row,False)
        COUNT_ROWs=COUNT_ROWs+1

def httpGetResultCheck(test_rep,xlw,list,xls_row):
    global COUNT_ROWs
    test_rep=json.loads(test_rep)
    result=test_rep['status']
    if(result == 1):
        xlw.write_HttpResult(COUNT_ROWs,list,xls_row,True)
        COUNT_ROWs=COUNT_ROWs+1
    else:
        Logging.writeHttpLog(str(list[xls_row][1]),str(test_rep))
        xlw.write_HttpResult(COUNT_ROWs,list,xls_row,False)
        COUNT_ROWs=COUNT_ROWs+1
View Code

以上就是所有的代碼添加:

執行后結果輸出見下圖:

其中執行失敗的用例,會以紅色醒目標識出來,並在其后輸出具體的中文用例描述

如有問題,歡迎交流


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM