python接口自动化框架搭建_python接口自动化框架搭建

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

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

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

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

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

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

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

二、下面是实现的思路:

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

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

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

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

关联示例:

1530528-20191031155951739-1001613815.png

参数配置示例:

1530528-20191031160137085-1017723370.png

日志示例:

1530528-20191031160328427-560030011.png

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值