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通过RabbitMQ服务器实现交换机功能的实例教程
Jun 29 Python
Python的Tornado框架实现图片上传及图片大小修改功能
Jun 30 Python
python中requests小技巧
May 10 Python
python实现简易版计算器
Jun 22 Python
Python生成一个迭代器的实操方法
Jun 18 Python
详解Python二维数组与三维数组切片的方法
Jul 18 Python
pygame实现非图片按钮效果
Oct 29 Python
Python并发请求下限制QPS(每秒查询率)的实现代码
Jun 05 Python
python删除指定列或多列单个或多个内容实例
Jun 28 Python
python中的装饰器该如何使用
Jun 18 Python
python语言中pandas字符串分割str.split()函数
Aug 05 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
Thinkphp搜索时首页分页和搜索页保持条件分页的方法
2014/12/05 PHP
学习php设计模式 php实现模板方法模式
2015/12/08 PHP
PHP设计模式之原型设计模式原理与用法分析
2018/04/25 PHP
PHP实现将base64编码字符串转换成图片示例
2018/06/22 PHP
PHP使用openssl扩展实现加解密方法示例
2020/02/20 PHP
自动更新作用
2006/10/08 Javascript
推荐一个封装好的getElementsByClassName方法
2014/12/02 Javascript
javascript结合canvas实现图片旋转效果
2015/05/03 Javascript
网页收藏夹显示ICO图标(代码少)
2015/08/04 Javascript
浅析创建javascript对象的方法
2016/05/13 Javascript
不间断循环滚动效果的实例代码(必看篇)
2016/10/08 Javascript
js清除浏览器缓存的几种方法
2017/03/15 Javascript
axios 处理 302 状态码的解决方法
2018/04/10 Javascript
最后说说Vue2 SSR 的 Cookies 问题
2018/05/25 Javascript
微信小程序签到功能
2018/10/31 Javascript
微信小程序生成二维码的示例代码
2019/03/29 Javascript
webpack 动态批量加载文件的实现方法
2020/03/19 Javascript
python算法学习之桶排序算法实例(分块排序)
2013/12/18 Python
Python 列表list使用介绍
2014/11/30 Python
Python解析json文件相关知识学习
2016/03/01 Python
Python迭代器与生成器用法实例分析
2018/07/09 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
2018/07/18 Python
Python正则表达式指南 推荐
2018/10/09 Python
对Python3中dict.keys()转换成list类型的方法详解
2019/02/03 Python
Django之提交表单与前后端交互的方法
2019/07/19 Python
python函数装饰器之带参数的函数和带参数的装饰器用法示例
2019/11/06 Python
Python迭代器Iterable判断方法解析
2020/03/16 Python
Python3 hashlib密码散列算法原理详解
2020/03/30 Python
python中time包实例详解
2021/02/02 Python
HTML5 CSS3实现一个精美VCD包装盒个性幻灯片案例
2014/06/16 HTML / CSS
远程Wi-Fi宠物监控相机:Petcube
2017/04/26 全球购物
实习会计求职自荐信范文
2014/03/10 职场文书
升学宴答谢词
2015/01/05 职场文书
道歉的话语大全
2015/05/12 职场文书
聊聊pytorch测试的时候为何要加上model.eval()
2021/05/23 Python
通过Qt连接OpenGauss数据库的详细教程
2021/06/23 PostgreSQL