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


猜你喜欢
- 本文实例讲述了Python简单I/O操作。分享给大家供大家参考,具体如下:文件:poem = '''hellowor
- vue组件在prop里根据type决定传值还是传引用。简要如下:传值:String、Number、Boolean传引用:Array、Obje
- 这是借鉴了一位兄弟的代码,然后进行修改的,原来代码存在问题,用了2小时,自己修改,终于画出了滑稽脸,也算是对于今天学的turtle绘画库的一
- 首先这是一个先有鸡还是先有蛋的问题,大部分时候数据都来自excel的整理,当数据越来越多,需要分析的点也越来越多的时候,通过excel来管理
- 使用Sublime Text做开发工具的小伙伴又有版本可以更新啦!费话不多说先把Sublime Text4注册码贴出来Sublime Tex
- 如何将123456789转化成123,456,789这样的形式呢?很多流量大的站比如优酷都有这样的格式。也是设计程序最常用的算
- 今天在百度知道看php相关资料时发现一个网友问一个wordpress安装提示错误的一个问题,说他在安装wordpress安装包时遇到这样一个
- 微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中。内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券
- 本文实例为大家分享了python抖音表白程序的具体代码,供大家参考,具体内容如下import sysimport randomimport
- 官方文档https://developers.weixin.qq.com/miniprogram/dev/devtools/download
- 本文实例讲述了Laravel框架用户登陆身份验证实现方法。分享给大家供大家参考,具体如下:laravel中检测用户是否登录,有以下的代码:i
- 目录1、面向对象基本概念1.1 万物皆对象1.2 面向对象编程1.3 面向对象的特征2、Python面向对象的术语3、Python类的构建3
- 一、前期准备1.设置GPUimport torchfrom torch import nnimport torchvisionfrom to
- 本文介绍了python BlockingScheduler定时任务及其他方式的实现,具体如下:#BlockingScheduler定时任务f
- 前言我们把可能发生错误的语句放在try模块里,用except来处理异常。except可以处理一个专门的异常,也可以处理一组圆括号中的异常,如
- 今天写一个在windows下批量修改文件名的python脚本,用到文件的遍历。用python进行文件遍历有多种方法,这里列举并说明一下。os
- 在开发T-SQL时,经常会需要对字符串进行各种各样的操作,下面介绍常用的字符串函数。一、编码转换1、获取字符的ASCII码:asciiASC
- XML虽然比JSON复杂,在Web中应用也不如以前多了,不过仍有很多地方在用,所以,有必要了解如何操作XML。DOM vs SAX操作XML
- 拉勾网面试题1. 你首先说出三个不同的特殊数,要求必须是个位数,比如3、5、7。2. 让所有学生拍成一队,然后按顺序报数。3. 学生报数时,
- Asp中Server.ScriptTimeOut属性需要注意的一点Server.ScriptTimeout?这个属性给定Asp脚本执行的最大