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)

    修改后执行结果:

  • 相关阅读:
    从C,C++,JAVA和C#看String库的发展(一)----C语言和C++篇
    Intent----android中的伟大邮差
    一步一步掌握线程机制(五)---等待与通知机制
    一步一步掌握线程机制(四)---同步方法和同步块
    一步一步掌握线程机制(三)---synchronized和volatile的使用
    利用单例模式解决全局访问问题
    一步一步掌握java的线程机制(二)----Thread的生命周期
    一步一步掌握java的线程机制(一)----创建线程
    如何快速学会android的四大基础----Service篇
    前端工程师必备实用网站
  • 原文地址:https://www.cnblogs.com/huwang-sun/p/11201907.html
Copyright © 2011-2022 走看看