zoukankan      html  css  js  c++  java
  • 自定义HTMLTestRunner报告case名称

    ddt.py源码中的mk_test_name函数是用来生成测试用例名字的

    参数:name、value、index

      name为单元测试中,测试用例的名字。即test_api

      value为测试数据。ddt是处理一组测试数据。而这个value就是这一组数据中的每一个测试数据

        对value的值是有限制的:要么就是单值变量,要么就是元组或者列表并且要求元组和列表中的数据都是单值变量。如("name","port") 、["name","port"]

      index:用例数量

        ddt源码中针对index设置了最大值index_len,默认为5,如果用例数大于5,需要修改该值

    mk_test_name函数源码:

    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)

    修改后():

    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):  # 如果不符合value的要求,则直接返回用例名称_下标作为最终测试用例名字。
            return "{0}_{1}".format(name, index)
        # 如果数据是list,则获取字典当中第一个数据作为测试用例名称
        if type(value) is list:
            try:
                value = value[0]
            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)

    修改后执行结果:

  • 相关阅读:
    SpringMVC文件上传
    c函数调用过程原理及函数栈帧分析
    《C++游戏开发》笔记十一 平滑动画:不再颤抖的小雪花
    Java学习笔记——IO操作之以图片地址下载图片
    uva 400 Unix ls 文件输出排版 排序题
    【VC++积累】之八、PreTranslageMessage;TranslageMessage;GetMessage和PeekMessage的区别
    uva 331 Mapping the Swaps 求交换排序的map 纯DFS
    uva 10344 23 out of 5 凑运算结果 全排列+dfs
    Java学习笔记——File类文件管理及IO读写、复制操作
    uva 301 Transportation 铁路公司的阳谋 纯dfs暴力
  • 原文地址:https://www.cnblogs.com/huwang-sun/p/11201907.html
Copyright © 2011-2022 走看看