ddt框架优化(生成html报告注释内容传变量)

https://blog.csdn.net/weixin_33923148/article/details/86017742

按要求修改后发现  注释只传值第一个变量

这是因为

ddt数据驱动生成html测试报告获取不到接口的名字

需要修改ddt中的 mk_test_name() 方法

#原文件方法
def mk_test_name(name, value, index=0):
    # Add zeros before index to keep order
    index = "{0:0{1}}".format(index + 1, index_len)
    if not is_trivial(value):
        return "{0}_{1}".format(name, index)
    try:
        value = str(value)
    except UnicodeEncodeError:
        # fallback for python2
        value = value.encode('ascii', 'backslashreplace')
    test_name = "{0}_{1}_{2}".format(name, index, value)
    return re.sub(r'W|^(?=d)', '_', test_name)

改为

def mk_test_name(name, value, index=0):
    # Add zeros before index to keep order
    index = "{0:0{1}}".format(index + 1, index_len)
    if not is_trivial(value) and type(value) is not dict:
        return "{0}_{1}".format(name, index)
    if type(value) is dict:
        try:
            value = value["case_name"]
            print("ddts-name->{}".format(value))
        except:
            return "{0}_{1}".format(name, index)
    try:
        value = str(value)
    except UnicodeEncodeError:
        # fallback for python2
        value = value.encode('ascii', 'backslashreplace')
    test_name = "{0}_{1}_{2}".format(name, index, value)
    return re.sub(r'W|^(?=d)', '_', test_name)

2、使用python使用ddt后,在生成的测试报告中,显示dict() -> new empty dictionary

修改ddt文件中的ddt方法,test_docstring = " " 这样就ok了

def ddt(cls):
    for name, func in list(cls.__dict__.items()):
        if hasattr(func, DATA_ATTR):
            for i, v in enumerate(getattr(func, DATA_ATTR)):
                test_name = mk_test_name(name, getattr(v, "__name__", v), i)
                test_docstring = getattr(v, "__doc__", None)
                test_docstring = ""  #临时解决报告出现dict()new empty dictionary问题
                if hasattr(func, UNPACK_ATTR):
                    if isinstance(v, tuple) or isinstance(v, list):
                        add_test(cls, test_name, test_docstring, func, *v)
                    else:
                        # unpack dictionary
                        add_test(cls, test_name, test_docstring, func, **v)
                else:
                    add_test(cls, test_name, test_docstring, func, v)
            delattr(cls, name)
        elif hasattr(func, FILE_ATTR):
            file_attr = getattr(func, FILE_ATTR)
            process_file_data(cls, name, func, file_attr)
            delattr(cls, name)
    return cls

或者 按照

https://www.cnblogs.com/yoyoketang/p/9719509.html

所说 重新安装 ddt 1.12

 

原文地址:https://www.cnblogs.com/clemente/p/10407453.html