码迷,mamicode.com
首页 > 编程语言 > 详细

Python--比较两个字典部分value是否相等(可以用于接口自动化)

时间:2020-02-28 15:47:19      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:前置   amp   代码   rom   table   span   技术   __name__   名称   

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","137",shop_name = shop_name,shop_short_name = shop_short_name,shop_phone = shop_phone,province = province,city = city,district = district,town = town,address = address)

 

Python--比较两个字典部分value是否相等(可以用于接口自动化)

标签:前置   amp   代码   rom   table   span   技术   __name__   名称   

原文地址:https://www.cnblogs.com/wangxiaoqun/p/12377252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!