Python--比较两个字典部分value是否相等(可以用于接口自动化)
2024-09-05 11:12:55
eg:例如你调用了一个新增的接口,以往功能测试的话,你再web端新增一个店铺之后,你肯定要去数据库中查看,这些数据插入的对不对,是否正确的插入了每个字段
# 比较两个字典部分是否相等
def compare_two_dict(dict1, dict2, key_list):
flag = True
keys1 = dict1.keys()
keys2 = dict2.keys()
if len(key_list) != 0:
for key in key_list:
if key in keys1 and key in keys2:
if dict1[key] == dict2[key]:
flag = flag & True
else:
flag = flag & False
else:
raise Exception('key_list contains error key')
else:
raise Exception('key_list is null')
if flag:
result = 'PASS'
else:
result = 'FAILED'
return result if __name__ == '__main__':
dict1 = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
}
dict2 = {
'a': 1,
'b': 2,
'c': 3,
'd': 8
}
key_list = ['a', 'c', 'b', 'd']
result = compare_two_dict(dict1, dict2, key_list)
print(result)
按照上面的列子我写出了自己的代码:前置条件:
但是有个前提条件:
'''
table:表名
key:要查找的字段名称
value:要查找的字段值
**kwargs:要检验的字段值,通过我新增的数据与数据库中的对比
例如你调用了一个新增的接口,以往功能测试的话,你再web端新增一个店铺之后,你肯定要去数据库中查看,这些数据插入的对不对,是否正确的插入了每个字段
'''
def assert_add_contents(db,cursor,table,key,value,**kwargs):
sql = 'select * from {} where {}={}'.format(table,key,value)
num = cursor.execute(sql)
result = cursor.fetchall()
print("查找到%d条数据" % num)
print("数据库查询数据为:{}".format(result))
# 这个是要对比的字段
field_list = list(kwargs.keys())
if num > 0:
print("kwargs:{}".format(kwargs))
print("result:{}".format(result[0]))
flag = True
keys1 = kwargs.keys()
keys2 = result[0].keys()
if len(field_list) != 0:
for key in field_list:
if key in keys1 and key in keys2:
if kwargs[key] == result[0][key]:
flag = flag & True
else:
flag = flag & False
else:
raise Exception('key_list contains error key')
else:
raise Exception('key_list is null')
if flag:
result = 'PASS'
else:
result = 'FAILED'
print("result============================={}".format(result))
return result
else:
pass db = config.db
cursor = db.cursor(cursor = pymysql.cursors.DictCursor) param = PostParameter().post_param(path,"shop","add_shop.json", "case001")
print("param[data]:{}".format(param["data"]))
shop_name = param["data"]["shop_name"]
shop_short_name = param["data"]["shop_short_name"]
shop_phone = param["data"]["shop_phone"]
province = param["data"]["province"]
city = param["data"]["city"]
district = param["data"]["district"]
town = param["data"]["town"]
address = param["data"]["address"] assert_add_contents(db,cursor,"t_shop_info","id","",shop_name = shop_name,shop_short_name = shop_short_name,shop_phone = shop_phone,province = province,city = city,district = district,town = town,address = address)
最新文章
- 反编译.NET工程
- Maven生命周期小记
- spring-data-redis 用法
- Unity5版本的AssetBundle打包方案之打包Scene场景
- JMeter学习(六)集合点
- MongoDB查询并更新一粟
- B-tree解释
- PHP负载均衡
- JavaScript高级程序设计(学习笔记)
- codeforces 316F3 Suns and Rays
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
- koa2 controller中实现类似sleep的延迟功能
- Template Method 模板设计模式
- 转摘: MySQL详解--锁
- 使用IdentityServer4,在一个ASPNetCore项目中,配置oidc和api的AccessToken两种认证授权
- 【.NET架构】BIM软件架构01:Revit插件产品架构梳理
- laravel5 报错419,form 添加crrf_field 后让然失败,本地环境配置问题
- 黑马day17 ajax&;amp;实现username自己主动刷新
- Linux下DIR,dirent,stat等结构体详解(转)
- 一:My Batis快速入门