接口自动化测试逻辑

接口固定数据检验

import json
import logging

def validate(rlt, expect):
    """
    结果效验
    忽略效验:如 "address":"pass"
    :param rlt: dict 返回值
    :param expect: json 预期值
    :return:
    """
    exp = json.loads(expect)
    r_lst, v_lst = [], []
    raw_lst = _recursion_data(data=rlt, rlt=r_lst)
    print(raw_lst)
    ver_lst = _recursion_data(data=exp, rlt=v_lst)
    for k in ver_lst:
        if k in raw_lst:
            print('验证ok:原始数据:%s 验证数据:%s' % (k, k))
        elif 'pass' in k.values():
            key_check = list(k.keys())[0]
            g_key = []
            for g in raw_lst:
                g_key.append(list(g.keys())[0])
            if key_check in g_key:
                print('验证ok:原始数据:%s 验证数据:%s' % (key_check, key_check))
            else:
                raise Exception('验证失败:未匹配到验证数据:原始数据:%s 验证数据:%s' % (g_key, key_check))

        else:
            raise Exception('验证失败:未匹配到验证数据:原始数据:%s 验证数据:%s' % (raw_lst, k))

def _recursion_data( data, rlt):
        """
        递归
        :param data: 预期值
        :param rlt:
        :return:
        """
        # data = json.loads(expect)
        for k in data:
            if isinstance(data[k], dict):
                _recursion_data(data[k], rlt)
            elif isinstance(data[k], list):
                for x in data[k]:
                    if type(x) == str or type(x) == int:
                        rlt.append({k, x})
                    else:
                        _recursion_data(x, rlt)
            else:
                rlt.append({k: data[k]})

        return rlt

if __name__ == '__main__':
    rlt_data ={"code": "01000000000", "errorMsg": "网络繁忙,请稍后重试",'namger':[{"name":"xiaoming","age":16},{"name":"xiaohong","age":20}],"fruites":[1,2,3]}
    except_data = '{"code": "01000000000", "errorMsg": "网络繁忙,请稍后重试","a":"pass"}'
    validate(rlt_data, except_data)

请求数据需要查询sql

新增接种反馈
    [Documentation]    测试使用
    #自定义变量    #关键字    #参数
    ${id}   db select  db_name=db_feedback    tab_index=0     sql= SELECT id FROM inoculate_feedback WHERE user_id= 5129535 ORDER BY create_time DESC LIMIT 0,1
    ${id1}  evaluate  ${id}+1
    ${params}    Set Variable    {"birthdate":"2014-04-10 00:00:00","childId":5000001208,"childName":"张七十二","descr":"无","gender":1,"hospitalId":20236,"indurationType":0,"inoculationTime":"2020-08-10 09:17:29","regionId":330302,"swellingType":0,"tempType":2,"userName":"*****","vccList":[{"vccId":"0201","vccIdx":1,"vccIdxNum":3,"vccName":"乙肝疫苗3"}]}
    ${validate}    Set Variable    {"code":"00000","data":${id1}}
    ${data}    Create Dictionary    url=${api}    deviceCode=${deviceCode}    params=${params}    account=${account}    validate=${validate}
    ${rlt}    Http Post    data=${data}
 def http_post(self, data):
        """
        post http
        请求参数中,无需带基础参数:sign timestamp, 如果只做字段验证 格式:{"code":"00000","data":{"authId": "pass"}}
        :param data:
        :return:
       data = {
            'account': {},  # 账号密码
            'url': '/user/login2',
            'deviceCode': '',  # headers -> deviceCode
            'params': '',  # 接口传参
            'validate': ''  # 验证内容
        }
        """
posted @ 2023-02-09 16:33  我是小菜鸡丫丫  阅读(22)  评论(0编辑  收藏  举报