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 相关文章推荐
使用scrapy实现爬网站例子和实现网络爬虫(蜘蛛)的步骤
Jan 23 Python
编写Python的web框架中的Model的教程
Apr 29 Python
Python中的hypot()方法使用简介
May 18 Python
python定时执行指定函数的方法
May 27 Python
MAC中PyCharm设置python3解释器
Dec 15 Python
用python制作游戏外挂
Jan 04 Python
图解Python变量与赋值
Apr 03 Python
解决python2 绘图title,xlabel,ylabel出现中文乱码的问题
Jan 29 Python
python-pyinstaller、打包后获取路径的实例
Jun 10 Python
Python 50行爬虫抓取并处理图灵书目过程详解
Sep 20 Python
python 实现多线程下载视频的代码
Nov 15 Python
Python学习之os模块及用法
Jun 03 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
把77A收信机改造成收音机
2021/03/02 无线电
PHP curl_setopt()函数实例代码与参数分析
2011/06/02 PHP
php写的简易聊天室代码
2011/06/04 PHP
Memcached常用命令以及使用说明详解
2013/06/27 PHP
PHP获取数组中指定的一列实例
2017/12/27 PHP
PHP+RabbitMQ实现消息队列的完整代码
2019/03/20 PHP
基于PHP实现生成随机水印图片
2020/12/09 PHP
JavaScript 对象模型 执行模型
2010/10/15 Javascript
浅谈bootstrap源码分析之scrollspy(滚动侦听)
2016/06/06 Javascript
AngularJS ng-bind 指令简单实现
2016/07/30 Javascript
AngularJS入门教程之更多模板详解
2016/08/19 Javascript
JavaScript toUpperCase()方法使用详解
2016/08/26 Javascript
js图片延迟加载(Lazyload)三种实现方式
2017/03/01 Javascript
js与jQuery实现的用户注册协议倒计时功能实例【三种方法】
2017/11/09 jQuery
vue+vue-validator 表单验证功能的实现代码
2017/11/13 Javascript
jquery根据name取得select选中的值实例(超简单)
2018/01/25 jQuery
使用vue-route 的 beforeEach 实现导航守卫(路由跳转前验证登录)功能
2018/03/22 Javascript
Element-UI踩坑之Pagination组件的使用
2018/10/29 Javascript
layui 对弹窗 form表单赋值的实现方法
2019/09/04 Javascript
js代码实现轮播图
2020/05/04 Javascript
vue使用vue-quill-editor富文本编辑器且将图片上传到服务器的功能
2021/01/13 Vue.js
[00:10]DOTA2全国高校联赛速递
2018/05/30 DOTA
python和C语言混合编程实例
2014/06/04 Python
python3实现字符串的全排列的方法(无重复字符)
2018/07/07 Python
浅谈python写入大量文件的问题
2018/11/09 Python
python实现播放音频和录音功能示例代码
2018/12/30 Python
pytorch多GPU并行运算的实现
2019/09/27 Python
Jupyter notebook设置背景主题,字体大小及自动补全代码的操作
2020/04/13 Python
日本PLST在线商店:日本时尚杂志刊载的人气服装
2016/12/10 全球购物
后勤园长自我鉴定
2013/10/17 职场文书
自荐书模板
2013/12/19 职场文书
25岁生日感言
2014/01/13 职场文书
环卫工作汇报材料
2014/10/28 职场文书
特种设备安全管理制度
2015/08/06 职场文书
祝福语集锦:给百岁老人祝寿贺词
2019/11/19 职场文书
Java存储没有重复元素的数组
2022/04/29 Java/Android