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)

    修改后执行结果:

  • 相关阅读:
    不知道是不是爬虫
    springCloud 搭建Eureka
    HttpsUtils
    java验证
    复选框值存数据库 存取问题
    Oracle VM VirtualBox 无法链接本地
    ssh 无法查询数据库
    CSS 分割线
    vue+Element 表格编辑
    数组,对象的深拷贝 与 浅拷贝
  • 原文地址:https://www.cnblogs.com/huwang-sun/p/11201907.html
Copyright © 2011-2022 走看看