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 sys模块sys.path使用方法示例
Dec 04 Python
跟老齐学Python之再深点,更懂list
Sep 20 Python
编写Python的web框架中的Model的教程
Apr 29 Python
python中map()与zip()操作方法
Feb 27 Python
python实现数据预处理之填充缺失值的示例
Dec 22 Python
Python之lambda匿名函数及map和filter的用法
Mar 05 Python
Python自定义函数计算给定日期是该年第几天的方法示例
May 30 Python
手把手教你pycharm专业版安装破解教程(linux版)
Sep 26 Python
k-means 聚类算法与Python实现代码
Jun 01 Python
Windows下PyCharm配置Anaconda环境(超详细教程)
Jul 31 Python
Python中免验证跳转到内容页的实例代码
Oct 23 Python
Django扫码抽奖平台的配置过程详解
Jan 14 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
Parse正式发布开源PHP SDK
2014/08/11 PHP
php+mysql实现的二级联动菜单效果详解
2016/05/10 PHP
基于jquery实现的鼠标拖拽元素复制并写入效果
2011/08/23 Javascript
javascript延时加载之defer测试
2012/12/28 Javascript
Nodejs中读取中文文件编码问题、发送邮件和定时任务实例
2015/01/01 NodeJs
跨域资源共享 CORS 详解
2016/04/26 Javascript
基于vuejs+webpack的日期选择插件
2020/05/21 Javascript
JavaScript中常见的八个陷阱总结
2017/06/28 Javascript
JS随机排序数组实现方法分析
2017/10/11 Javascript
Angular搜索场景中使用rxjs的操作符处理思路
2018/05/30 Javascript
layer弹出层 iframe层去掉滚动条的实例代码
2018/08/17 Javascript
Javascript实现一朵从含苞到绽放的玫瑰
2019/03/30 Javascript
JS防抖和节流实例解析
2019/09/24 Javascript
微信小程序上传图片并等比列压缩到指定大小的实例代码
2019/10/24 Javascript
JavaScript 面向对象程序设计详解【类的创建、实例对象、构造函数、原型等】
2020/05/12 Javascript
vue+node 实现视频在线播放的实例代码
2020/10/19 Javascript
使用Python抓取模板之家的CSS模板
2015/03/16 Python
Python实现快速排序算法及去重的快速排序的简单示例
2016/06/26 Python
Python 实现引用其他.py文件中的类和类的方法
2018/04/29 Python
python 实现提取某个索引中某个时间段的数据方法
2019/02/01 Python
python3中替换python2中cmp函数的实现
2019/08/20 Python
postman传递当前时间戳实例详解
2019/09/14 Python
python+gdal+遥感图像拼接(mosaic)的实例
2020/03/10 Python
在python中使用pyspark读写Hive数据操作
2020/06/06 Python
pycharm中使用request和Pytest进行接口测试的方法
2020/07/31 Python
Django集成MongoDB实现过程解析
2020/12/01 Python
基于python+selenium自动健康打卡的实现代码
2021/01/13 Python
英国巧克力贸易公司:Chocolate Trading Company
2017/03/21 全球购物
Notino匈牙利:购买香水和化妆品
2019/04/12 全球购物
车工岗位职责
2013/11/26 职场文书
个人对照检查材料
2014/02/12 职场文书
大学英语演讲稿范文
2014/04/24 职场文书
教师党的群众路线教育实践活动个人整改措施
2014/11/04 职场文书
2015年教师教学工作总结
2015/04/28 职场文书
2015年大学教师工作总结
2015/05/20 职场文书
读《工匠精神》有感:热爱工作,精益求精
2019/12/28 职场文书