ApiFrame--参数替换

接口传参替换

  • 在python接口自动化测试过程中,通常会先从数据库或数据文件中读取接口入参,但存在部分动态参数或随机参数,此时需要对参数进行二次替换。

  • 读取的入参格式我们希望都是字典格式,但是由于开发的不规范或者数据的特殊性,数据有可能是字典嵌套字典、列表或者被包引号的字典("{}")等,此时我们希望仍用一层字典去对数据进行替换。

字典替换字典

def replace_dict_by_dict(org_dict, replace_dict):
    """
    检查replace_dict中的key,如果与org_dict中的key值相同,则把org_dict中该key对应的value值替换成replace_dict中的value值
    :param org_dict: 原始字典
    :param replace_dict: 替换字典
    :return: 修改后的原始字典,key值不变,value值需从replace_dict中寻找匹配
    """

    if isinstance(org_dict, (list, tuple)):
        list_temp = list()
        for item in org_dict:
            list_temp.append(replace_dict_value(item, replace_dict))    # [{},{}] 、['123', 123, 'qwer']
        return list_temp
    if isinstance(org_dict, str):
        try:
            org_dict = str(replace_dict_value(eval(org_dict), replace_dict))   # '[{},{}]'、'['123', 123, qwer]'、'{}'
        except:
            org_dict = replace_dict.get(str(org_dict)) or org_dict              # '123'、 'qwer'
        finally:
            return org_dict
    if isinstance(org_dict, dict):
        for key, value in org_dict.items():
            org_dict[key] = replace_dict_value(value, replace_dict)
        return org_dict
    return org_dict


if __name__ == '__main__':
    old = {'a': 'a', 'key1': [{'key1': 'key1', 'key2': 'key2'}, {'key5': '555', 'key6': '666'}], 'c': "{'key3': 'key3', 'key4': 'key4'}"}
    m = {'c': "{'key3': 'key3', 'key4': '444'}"}
    n = {'c': "['key3', 'key4', 'key5', 123]"}
    new = {'a': 'n_123', 'key1': 'n_111', 'key2': 'n_222', 'key3': 'n_333', 'key4': 'n_444'}

    res = replace_dict_value(old, new)
    print(res)
    res = replace_dict_value(m, new)
    print(res)
    res = replace_dict_value(n, new)
    print(res)


字典替换字符串

import re


def search_variable(str_data: str, pattern=r"\$\{(\w+)\}|\$(\w+)"):
    """
    判断是否存在$var或${var}格式的变量
    :param str_data:  str
    :param pattern:  str
    :return:  str
    """
    pattern = re.compile(pattern)
    if pattern.search(str_data):
        return pattern.search(str_data).start()
    return False


def replace_str_by_dict(str_data: str, dict_data: dict, count=1):
    """
    使用dict去替换带有$var或${var}的字符串
    :param str_data:  str
    :param dict_data:  dict
    :param count:  num  > 1: 标识每次替换1个, 0: 全部替换
    :return:  str
    """
    for key, value in dict_data.items():
        variable_regex_compile = r"\${{{0}}}|\${0}".format(key)
        while search_variable(str_data, pattern=variable_regex_compile):
            start = search_variable(str_data, pattern=variable_regex_compile)
            str_data = re.sub(variable_regex_compile, str(value), str_data, count=count)
            if isinstance(value, int) and \
                    str_data[start - 1] == str_data[start + len(str(value))] and \
                    str_data[start - 1] in ('"', "'"):
                str_data = str_data[0:start - 1] + \
                           str_data[start: start + len(str(value))] + \
                           str_data[start + len(str(value)) + 1:]
    return str_data


if __name__ == '__main__':
    old = {'a': '${a}', 'key1': [{'key1': '${key1}', 'key2': 'key2'}, {'key5': '555', 'key6': '666'}], 'c': "{'key3': 'key3', 'key4': '$key4'}"}
    m = {'c': "{'key3': 'key3', 'key4': '${key4}'}"}
    n = {'c': "['key3', '${key4}', '$key5', 123]"}
    new = {'a': 'n_123', 'key1': 'n_111', 'key2': 'n_222', 'key3': 'n_333', 'key4': 'n_444', 'key5': 'n_555'}

    res = replace_str_by_dict(str(old), new)
    print(res)
    res = replace_str_by_dict(str(m), new)
    print(res)
    res = replace_str_by_dict(str(n), new)
    print(res)

>{'a': 'n_123', 'key1': [{'key1': 'n_111', 'key2': 'key2'}, {'key5': '555', 'key6': '666'}], 'c': "{'key3': 'key3', 'key4': 'n_444'}"}
>{'c': "{'key3': 'key3', 'key4': 'n_444'}"}
>{'c': "['key3', 'n_444', 'n_555', 123]"}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值