zoukankan      html  css  js  c++  java
  • ddt+excel接口自动化测试-HtmlTestRunner报告中用例名称显示excel中自定义的名称

    最近的几个自动化项目,全都是用的unittest+excel+ddt来进行数据驱动的测试,使用HtmlTesttRunnerCN来生成美化后的测试用例,但是在用例名称的显示方面碰到点问题,

    用例名称都是类似于:test_api_index.index表示用例的编号,从1开始递增,test_api_01、test_api_02.....。

    记录一下问题解决方案。

    最终结果类似于下图:

    1.把excel中的用例名称描述,作为测试报告中的用例名称显示出来。

     测试用例名称为excel中自定义的名称。

    打开ddt.py的源码,ddt获取测试用例名称的方法为 mk_test_name

    手动改一下它获取用例名称的方式,先判断读取的内容数据类型,再根据自己excel中字段的名称做对应修改即可。

    直接上代码(修改的位置已经在代码中标注了):

    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)
    
        # 添加了对字典数据的处理。以下为修改========================》
        if not is_trivial(value) and type(value) is not dict and type(value) is not list:
            return "{0}_{1}".format(name, index)
    
        # 如果数据是字典,则获取字典当中的api_name对应的值,加到测试用例名称中。
        if type(value) is dict:
            try:
                value = value["description"]  # 划重点,这里是需要提取的用例名称
            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)

    PS:还有个小问题,代码如果需要部署到其他机器上运行,导入ddt的时候会直接使用原版的ddt,所以需要把修改过后的ddt.py提取出来放到代码中,使用的时候直接引入本地的ddt就行。

  • 相关阅读:
    一个.NET通用JSON解析/构建类的实现(c#)
    WCF编程系列(六)以编程方式配置终结点
    WCF编程系列(三)地址与绑定
    WCF编程系列(一)初识WCF
    WCF编程系列(二)了解WCF
    SQL Server Management Studio 2005 打开的数据库是8.0的解决方法【原】
    GridView控件双击某行变色【整理】
    winform下comboBox控件绑定数据并设置其value【整理】
    网页上有错误,除了js,还有其他原因【原】
    Js将当前日期显示在浏览器的状态栏【搜藏】
  • 原文地址:https://www.cnblogs.com/mlp1234/p/13685998.html
Copyright © 2011-2022 走看看