python接口自动化框架搭建

一、在搭建接口自动化测试框架前,我觉得先需要想明白以下几点:

  ① 目前情况下,绝大部分接口协议是http,所以需要对http协议有个基本的了解,如:http协议请求、响应由哪些部分组成,常用的method,对应的请求传参方式等等

  ② 需要对接口发送请求,所以要对可以发送http请求的模块比较熟悉,如python 的requests、urllib 等

  ③ 使用的数据承载工具,如使用excel、mysql、oracle 等

  ④ 实现哪些需求,如 在用例层面控制是否执行用例,响应信息、执行结果、失败原因等等写入数据载体,可变参数分离的配置化,测试结束后邮件发送结果给相关人员等等

  ⑤ 发送请求前需要解决哪些问题,如 上下接口间的关联(包含请求参数与关联参数的映射关系)、url的拼接等等;请求后的断言等等

  ⑥ 其他的,如涉及到接口加密、调用其他语言的方法等等

 

二、下面是实现的思路:

先遍历接口列表》查找出需要测试的接口》根据接口找到对应的用例》

遍历该接口的用例》找出需要执行的用例》判断用例是否与其他接口有关联》

处理关联关系》拼接请求url及参数》发送请求》断言用例是否通过》写入结果内容》发送邮件

 

三、框架模块基本结构(数据载体使用excel)

关联示例:

 

 

参数配置示例:

 

 

 日志示例:

 

 

 

四、主函数详细代码(即第二步的思路实现)

from utils.ParseExcel import *
from config.PbulicConfigData import *
from action.GetRely import GetRely
from utils.HttpRequest import HttpRequest
from action.AssertResult import AsserResult
from utils.GetDateOrTime import GetDateOrTime
from utils.SendEmail import Carry_files_EmailSender
import time

def 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()

 

posted @ 2019-09-18 00:10  WhiteMouse  Views(3282)  Comments(0Edit  收藏  举报