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()
相关文章
相关标签/搜索