zoukankan      html  css  js  c++  java
  • Python--比较两个字典部分value是否相等(可以用于接口自动化)

    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)
  • 相关阅读:
    FastWeb2011互联网工具发布
    本站启用新型试用模式
    TDiode单相及三相整流电路辅助设计器发布
    DCOp直流多级电路计算机辅助设计软件下载
    OpDesign2阻容耦合放大电路辅助设计软件下载
    共享软件的思考
    IpAdr网络地址计算器2011发布
    使用JavaScript动态添加复选框Checkbox
    JQuery操作checkbox、radio
    如果Oracle中的字段和数据库的系统字段重名,怎么配置NHibernate的映射实体文件
  • 原文地址:https://www.cnblogs.com/wangxiaoqun/p/12377252.html
Copyright © 2011-2022 走看看