接口测试 有参数依赖时,请求参数的多层嵌套格式怎么处理?

testjson · 2022年12月26日 · 最后由 testjson 回复于 2022年12月28日 · 5049 次阅读

下面是处理请求参数格式为 [{},{}], 但是遇到其他的格式有参数依赖就无法处理了
例如 { { } } 这种格式

def request_data_nest_replace(access_value, dict_v):
    '''
    请求参数多层嵌套,处理嵌套里面的jsonpath表达式转为值,
    但是数字也会被变为字符串,加标识再写一个方法(int_replace_str)进行处理
    :access_value :参数池
    :param dict_v: 多层嵌套参数的值当前支持的格式[{},{}]
    :return: 多层请求参数被替换后的值
    '''
    replace_list = re.findall('^(.*?)^', str(dict_v))
    logger.info(f'多层请求参数中的替换表达式:{replace_list}')
    for i in replace_list:
        if '$.' in i:
            replace_value = jsonpath(access_value, i)
            if replace_value != False:
                replace_value = replace_value[0]
        if 'random' in i:
            replace_value = eval(i)
        bei_replace = f'^{i}^'  # '^$.waybillid^'
        if type(replace_value) == int:
            dict_v = str(dict_v).replace(bei_replace, 'int' + str(replace_value))
        else:
            dict_v = str(dict_v).replace(bei_replace, str(replace_value))
    new_dict_v = int_replace_str(eval(dict_v))
    return new_dict_v
共收到 2 条回复 时间 点赞

怎么改的能处理请求参数有其他格式的呢

哈哈,研究了会,终于搞定了
在调用的 int_replace_str 方法里面新增了段代码

elif isinstance(new_dict_v, dict):
    for i in new_dict_v:
        if isinstance(i, dict):
            for k, v in i.items():
                if v != None and type(v) != bool:
                    if 'int' in v:
                        #进行切片截取
                        new_v = v[3:len(v) + 1]
                        #将截取后的值赋值给i的键
                        i[k] = int(new_v)
    return new_dict_v
testjson 关闭了讨论 12月28日 18:20
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册