乐趣区

关于单元测试:unittest中使用ddt后生成的测试报告名称如何修改如testapi0修改成testapi0titile

批改前:

Unittest 应用 ddt 后生成的测试报告用例名称为:

即就是,以“test_xx_数字”为格局的用例名称,感觉满足不了咱们的测试需要,不够直观。那么怎么批改呢?

查看 ddt 源码

def mk_test_name(name, value, index=0):
    """
    Generate a new name for a test case.

    It will take the original test name and append an ordinal index and a
    string representation of the value, and convert the result into a valid
    python identifier by replacing extraneous characters with ``_``.

    We avoid doing str(value) if dealing with non-trivial values.
    The problem is possible different names with different runs, e.g.
    different order of dictionary keys (see PYTHONHASHSEED) or dealing
    with mock objects.
    Trivial scalar values are passed as is.

    A "trivial" value is a plain scalar, or a tuple or list consisting
    only of trivial values.
    """

    # 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)
    
  • 从办法 mk_test_name 中,咱们看到该办法的形容是“Generate a new name for a test case.”,即就是为测试用例创立一个名称,那么改这个办法就行了
  • 办法中返回的是 name 和 index,即 ”{0}_{1}”.format(name, index)
  • 那么就明确了,咱们改返回的内容就行了

批改后

def mk_test_name(name, value, index=0):
    """
    Generate a new name for a test case.

    It will take the original test name and append an ordinal index and a
    string representation of the value, and convert the result into a valid
    python identifier by replacing extraneous characters with ``_``.

    We avoid doing str(value) if dealing with non-trivial values.
    The problem is possible different names with different runs, e.g.
    different order of dictionary keys (see PYTHONHASHSEED) or dealing
    with mock objects.
    Trivial scalar values are passed as is.

    A "trivial" value is a plain scalar, or a tuple or list consisting
    only of trivial values.
    """

    # 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: # 减少的中央,减少 value 的字典判断

        return "{0}_{1}_{2}".format(name, index, value.name) # 批改的中央,减少返回的值
    if type(value) is dict: # 减少的中央
        try: # 减少的中央
            value = value["name"] + "_" + value["function"] # 减少的中央,name 和 function 必须是 execl 用例中整正存在的表头,这里我是把两个表头合并了(name 是我表格中接口的名称,function 是表格中接口的性能形容)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)

退出移动版