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


Posted in Python onFebruary 11, 2018

背景:之前写的接口测试一直没有支持无限嵌套对比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 相关文章推荐
Python字符串的encode与decode研究心得乱码问题解决方法
Mar 23 Python
Python ORM框架SQLAlchemy学习笔记之数据查询实例
Jun 10 Python
Python实现对excel文件列表值进行统计的方法
Jul 25 Python
利用Python读取文件的四种不同方法比对
May 18 Python
Python使用base64模块进行二进制数据编码详解
Jan 11 Python
python3+PyQt5使用数据库窗口视图
Apr 24 Python
python使用多进程的实例详解
Sep 19 Python
Python设计模式之命令模式原理与用法实例分析
Jan 11 Python
Python数据结构与算法(几种排序)小结
Jun 22 Python
python之pexpect实现自动交互的例子
Jul 25 Python
OpenCV哈里斯(Harris)角点检测的实现
Jan 15 Python
基于CentOS搭建Python Django环境过程解析
Aug 24 Python
使用Python读取大文件的方法
Feb 11 #Python
python脚本作为Windows服务启动代码详解
Feb 11 #Python
分析Python读取文件时的路径问题
Feb 11 #Python
Django中针对基于类的视图添加csrf_exempt实例代码
Feb 11 #Python
python jieba分词并统计词频后输出结果到Excel和txt文档方法
Feb 11 #Python
代码讲解Python对Windows服务进行监控
Feb 11 #Python
django 按时间范围查询数据库实例代码
Feb 11 #Python
You might like
为什么夜间收到的中波电台比白天多
2021/03/01 无线电
PHP实现文件上传与下载实例与总结
2016/03/13 PHP
Symfony2学习笔记之插件格式分析
2016/03/17 PHP
用javascript实现读取txt文档的脚本
2007/07/20 Javascript
推荐11款jQuery开发的复选框和单选框美化插件
2011/08/02 Javascript
JavaScript 函数replace深入了解
2013/03/14 Javascript
iframe的onreadystatechange事件在firefox下的使用
2014/04/16 Javascript
js判断游览器类型及版本号的代码
2014/05/11 Javascript
js表头排序实现方法
2015/01/16 Javascript
javascript实现仿IE顶部的可关闭警告条
2015/05/05 Javascript
JavaScript对数组进行随机重排的方法
2015/07/22 Javascript
JS实现的点击表头排序功能示例
2017/03/27 Javascript
vue对storejs获取的数据进行处理时遇到的几种问题小结
2018/03/20 Javascript
详解vue-cli项目开发/生产环境代理实现跨域请求
2019/07/23 Javascript
Vue关于组件化开发知识点详解
2020/05/13 Javascript
JavaScript实现随机点名小程序
2020/10/29 Javascript
微信小程序实现倒计时功能
2020/11/19 Javascript
Python随机生成均匀分布在单位圆内的点代码示例
2017/11/13 Python
python筛选出两个文件中重复行的方法
2018/05/31 Python
使用Python编写Prometheus监控的方法
2018/10/15 Python
python使用插值法画出平滑曲线
2018/12/15 Python
python接口自动化(十六)--参数关联接口后传(详解)
2019/04/16 Python
零基础使用Python读写处理Excel表格的方法
2019/05/02 Python
python3.x 生成3维随机数组实例
2019/11/28 Python
python实现tail实时查看服务器日志示例
2019/12/24 Python
浅谈在django中使用redirect重定向数据传输的问题
2020/03/13 Python
520使用Python实现“我爱你”表白
2020/05/20 Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
2021/01/12 Python
CSS3实现同时执行倾斜和旋转的动画效果
2016/10/27 HTML / CSS
四方通行旅游网:台湾订房、出国旅游
2017/09/20 全球购物
Ray-Ban雷朋西班牙官网:全球领先的太阳眼镜品牌
2018/11/28 全球购物
JAVA程序设计笔试题面试题一套
2015/07/28 面试题
大客户销售经理职责
2013/12/04 职场文书
党员违纪检讨书
2015/05/05 职场文书
2015年电厂工作总结范文
2015/05/13 职场文书
MySQL查询日期时间
2022/05/15 MySQL