python接口自動化框架搭建

1、在搭建接口自動化測試框架前,我以爲先須要想明白如下幾點:python

  ① 目前狀況下,絕大部分接口協議是http,因此須要對http協議有個基本的瞭解,如:http協議請求、響應由哪些部分組成,經常使用的method,對應的請求傳參方式等等mysql

  ② 須要對接口發送請求,因此要對能夠發送http請求的模塊比較熟悉,如python 的requests、urllib 等sql

  ③ 使用的數據承載工具,如使用excel、mysql、oracle 等api

  ④ 實現哪些需求,如 在用例層面控制是否執行用例,響應信息、執行結果、失敗緣由等等寫入數據載體,可變參數分離的配置化,測試結束後郵件發送結果給相關人員等等oracle

  ⑤ 發送請求前須要解決哪些問題,如 上下接口間的關聯(包含請求參數與關聯參數的映射關係)、url的拼接等等;請求後的斷言等等框架

  ⑥ 其餘的,如涉及到接口加密、調用其餘語言的方法等等函數

 

2、下面是實現的思路:工具

先遍歷接口列表》查找出須要測試的接口》根據接口找到對應的用例》測試

遍歷該接口的用例》找出須要執行的用例》判斷用例是否與其餘接口有關聯》加密

處理關聯關係》拼接請求url及參數》發送請求》斷言用例是否經過》寫入結果內容》發送郵件

 

3、框架模塊基本結構(數據載體使用excel)

關聯示例:

 

 

參數配置示例:

 

 

 日誌示例:

 

 

 

4、主函數詳細代碼(即第二步的思路實現)

from utils.ParseExcel import *from config.PbulicConfigData import *from action.GetRely import GetRelyfrom utils.HttpRequest import HttpRequestfrom action.AssertResult import AsserResultfrom utils.GetDateOrTime import GetDateOrTimefrom utils.SendEmail import Carry_files_EmailSenderimport timedef main():    parseE=ParseExcel(ExcelPathAndName)    #遍歷接口列表    wb=parseE.GetWorkBook()    for idx,cell in enumerate(parseE.GetColumns("API",API_active)[1:],2):        #print(idx,cell.value)        if cell.value=="y":            #print(ord(API_apiName)-64,API_apiName)            #ApiName=parseE.GetValueOfCell("API",columnNo=ord(API_apiName)-64,rowNo=idx)            RequestUrl=parseE.GetValueOfCell("API",columnNo=ord(API_requestUrl)-64,rowNo=idx)            RequestMothod=parseE.GetValueOfCell("API",columnNo=ord(API_requestMothod)-64,rowNo=idx)            ParamsType=parseE.GetValueOfCell("API",columnNo=ord(API_paramsType)-64,rowNo=idx)            ApiCaseSheet=parseE.GetValueOfCell("API",columnNo=ord(API_apiTestCaseFileName)-64,rowNo=idx)            #print(ApiName,RequestUrl,RequestMothod,ParamsType,ApiCaseSheet)            for i,c in enumerate(parseE.GetColumns(ApiCaseSheet,CASE_active)[1:],2):                #print(i,c.value)                if c.value=="y":                    RequestData=parseE.GetValueOfCell(ApiCaseSheet,columnNo=ord(CASE_requestData)-64,rowNo=i)                    RelyData=parseE.GetValueOfCell(ApiCaseSheet,columnNo=ord(CASE_relyData)-64,rowNo=i)                    CheckPoint=parseE.GetValueOfCell(ApiCaseSheet,columnNo=ord(CASE_checkPoint)-64,rowNo=i)            #依賴關係處理                    RequestData=GetRely(parseE,RequestData,RelyData)                    print("-----------處理依賴關係後的請求參數---------:",RequestData)                    print("-----------依賴關係---------:",RelyData)                    print( "-----------檢查點參數---------:",CheckPoint)                    Response=HttpRequest.request(RequestUrl,RequestMothod,ParamsType,spacer,requestData=RequestData)                    print("-------------------接口響應-----------------:",Response.text)                    Assertresult=AsserResult.CheckResult(Response.text,CheckPoint)                    print(Assertresult)                    testTime=GetDateOrTime.GetDates("-")            #寫入結果                    parseE.WriteValueInCell(ApiCaseSheet,Response.status_code,rowNo=i,columnNo=ord(CASE_responseCode)-64)                    parseE.WriteValueInCell(ApiCaseSheet,Response.text,rowNo=i,columnNo=ord(CASE_responseData)-64)                    print("-----------",Assertresult[1])                    if Assertresult[0]=="ture":                        parseE.WriteValueInCell(ApiCaseSheet, Assertresult[0], rowNo=i, columnNo=ord(CASE_status) - 64,colour="green")                    else:                        parseE.WriteValueInCell(ApiCaseSheet,str(Assertresult[1]), rowNo=i, columnNo=ord(CASE_failedReason)-64,colour="red")                        parseE.WriteValueInCell(ApiCaseSheet, Assertresult[0], rowNo=i, columnNo=ord(CASE_status) - 64,colour="red")                        parseE.WriteValueInCell(ApiCaseSheet, testTime, rowNo=i, columnNo=ord(CASE_testTime) - 64)        wb.save(ResultPathAndName)    time.sleep(10)  #發送郵件    if switch==1:        sender=Carry_files_EmailSender()        sender.send_email(to_email_list,subject,body,files_part=ResultPathAndName)if __name__=="__main__":    main()
相關文章
相關標籤/搜索