python 接口测试response返回数据对比的方法
作者:gogoboi_jin 发布时间:2023-10-12 14:01:31
背景:之前写的接口测试一直没有支持无限嵌套对比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天多,看着简单,自己写坑还是挺多的。
来源:http://blog.csdn.net/gogoboi_jin/article/details/73201604
猜你喜欢
- 如题,度娘前几条答案说的都不清不楚,俺来补上:点击下拉选项中的Edit Configuration进入如下界面:如果左侧没有出现django
- 这里我不想采用诸如ubuntu下的apt-get install方式进行python的安装,而是在linux下采用源码包的方式进行pytho
- pandas.DataFrame为每一列保存一个数据类型dtype。要仅提取(选择)特定数据类型为dtype的列,请使用pandas.Dat
- 请问论坛的树状记录表是怎么展开的?如何做?论坛的这种展开技术一般采用两种方法实现,一种是采用递归的方法,优点是逻辑简单,编程简单,缺点是速度
- 译序:本文提到了一种很不错的实现跨浏览器圆角的解决方案,但是说的不够全面,前端观察最近将整理更多更全面的资源给大家,敬请期待。前一段时间,我
- 元组是不可变的Python对象序列。元组的序列就像列表。唯一的区别是,元组不能被改变,即元组是不可被修改。元组使用小括号,而列表
- 我们在前面的几节中分别讲了提高网站性能中内容、服务器、JavaScript和CSS等方面的内容。除此之外,图片和Coockie也是我们网站中
- 平时在写asp代码的时候有很多重复的内容要写,麻烦的要命,比如在收集表单提交的数据时,特别是表单的输入域比较多时,要不断写好多的reques
- 一、函数list(1)定义:用打开的文件作为参数,把文件内的每一行内容作为一个元素(2)格式:list(文件)(3)例子:with open
- python3.6.4安装opencv3.4.2使用pip安装OpenCV直接安装最新版:pip3 install opencv_pytho
- 在我们的日常生活工作中,经常会遇到需要上传日志的场景,比如多台机器运行同一个程序,并且需要记录每台机器程序产生的日志,根据相关关键词告警,或
- 实例如下所示:# -*- coding: utf-8 -*-import xlrddef open_excel(file = 'fi
- 背景今天突然想到之前被要求做同性质银行的数据分析。妈耶!十几个银行,每个银行近5年的财务数据,而且财务报表一般都是 pdf 的,我们将 pd
- 目录1、面向对象基本概念1.1 万物皆对象1.2 面向对象编程1.3 面向对象的特征2、Python面向对象的术语3、Python类的构建3
- 因此计划先把数据转插入一个临时表,再对临时表的数据进行分析。 问题点是如何动态创建临时表。原先Insus.NET使用下面代码实现: 代码如下
- 设置密码保护SqlServer数据库备份文件! 备份SqlServer数据库 Backup Database [数据库] To disk=&
- 1:在终端下:mysql -V。 以下是代码片段:[shengting@login ~]$ mysql -Vmysql Ver 14.7 D
- 0. dockerfile命令FROM # 基础镜像,一切从这里开始构建MAINTAINER # 镜像是谁写的,姓名+邮箱RUN# 镜像构建
- SELECT语句具有种类繁多的各类选项,这些选项可以用来控制数据返回的方式。这些选项以子句、关键词和函数的形式存在。 子句是一种修改结果的语
- 年初的时候收藏过一篇关于mysqlreport的报表解读,和内置的show status,和show variables相比mysqlrep