侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python 接口测试response返回数据对比的方法

Python  /  管理员 发布于 7年前   222

背景:之前写的接口测试一直没有支持无限嵌套对比key,上次testerhome逛论坛,有人分享了他的框架,看了一下,有些地方不合适我这边自己修改了一下,部署在jenkins上跑完效果还不错,拿出来分享一下。ps:还是要多看看别人写的,新学了不少python自带的一些常用方法。

这次直接上代码,下面写一下这次我新学一些方法和思路。

def check_response_hope_key(self,response={},hope_response={}):  temp_data={}  for n1 in hope_response:   print "n1:",n1   #如果值是字典类型   if isinstance(hope_response[n1],dict):    print "dict"    if not Check_Response_Hope().check_response_hope_key(response=response.get(n1), hope_response=hope_response[n1]):     MailFile().checkfail(response=response.get(n1), hope_response=hope_response[n1])     return False     raise '{},{}'.format(hope_response[n1],response[n1])      #如果值是列表类型   elif isinstance(hope_response[n1],list):    print "list"    for hope_index,hope_listValue in enumerate(hope_response[n1]):     #print "hope_index:",hope_index     #print "hope_listValue:",hope_listValue     for response_index,response_listValue in enumerate(response[n1]):      #print "response_index:",response_index      #print "response_listValue:",response_listValue      if isinstance(hope_listValue,dict):       Check_Response_Hope().check_response_hope_key(response=response[n1][response_index],hope_response=hope_response[n1][response_index])      elif isinstance(hope_listValue,list):       if hope_response[n1][hope_index]==response[n1][hope_index]:        break       else:        MailFile().checkfail(response=response_listValue,hope=hope_listValue)        raise Exception ("hope_response="+str(hope_response[n1][hope_index])+"\n"+"response="+str(response[n1][response_index]))      else:       if hope_response[n1][hope_index]==response[n1][hope_index]:        break       else:        MailFile().checkfail(response=response[n1][hope_index],hope=hope_response[n1][hope_index])        raise Exception ("hope_response="+str(hope_listValue)+"\n"+"response="+str(response_listValue))   else:    print "string"    if response.has_key(n1):     continue    else:     temp_data['error_data']='{}:{},{}:{}'.format(n1,hope_response[n1],n1,response[n1])     #发送邮件     MailFile().checkfail(response=response[n1],hope=hope_response[n1])     raise Exception ("hope_response="+str(hope_response[n1])+"\n"+"response="+str(response.get(n1)))      return True

内置函数enumerate():

传入list的数据时返回该列表的索引和值,例如:

>>> list1=[1,2,3,4]>>> for list_index,list_value in enumerate(list1):...  print list_index,list_value... 

0 1
1 2
2 3
3 4

还可以控制索引的起始值开始迭代,例如:

>>> for list_index,list_value in enumerate(list1,1):...  print list_index,list_value... 

1 1
2 2
3 3
4 4

内置函数isinstance(object,type):

用于判断传入对象是什么类型,返回布尔类型true或false,例如:

>>> isinstance(list1,dict)False

ps:这个方法真的挺好用的,很基础可以根据返回的布尔类型走不同的if分支。

内置函数format()

这个函数作用就是格式化字符串,这里面不是非要用,我用完感觉还是挺方便的,结构也清晰,在下面举个常用例子。
1.通过位置进行映射:

>>> '{},{}'.format('abc',123)'abc,123'>>> '{1}{0}{1}'.format('abc',123)'123abc123'

2.通过下标

>>> list1=['a','b']>>> '{0[1]},{0[0]}'.format(list1)'b,a'

当然还其他很多用法,我也没用到,还是挺强大的,有兴趣自己百度一下吧,很多写的很详细。

思路:

接口返回response一定是字典格式的,因为我写的接口测试框架用的orm链接数据库动态从数据库中传参数,所以返回value可能会不同,但是返回response的key肯定是固定的,所以我这里验证所有的key。

首先遍历hope_response(期望接口返回),hope_response[n]可能类型字典,列表或者string/int(我目前没有见过key是int型的),所以使用isinsstance()去判断value的类型。如果是string就表示是最简单的一层{key:value}形式,这里就使用has_key来判断response中有没有该key。hope_response[n]是dict类型,就递归,最后一定会落到string/int类型的分支。如果hope_response[n]是list类型,就用到enumerate()来拿到索引和值,根据值的类型去判断。大体思路这样的,我调试1天多,看着简单,自己写坑还是挺多的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    python中数据爬虫requests库使用方法详解
    下一条:
    使用Python读取大文件的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客