美文网首页
ddt源码修改:HtmlTestRunner报告用例名称读取ca

ddt源码修改:HtmlTestRunner报告用例名称读取ca

作者: 别动我名字呀 | 来源:发表于2018-12-11 23:03 被阅读171次

    在网上查看了很多资料,搭建了一套测试框架用unittest + ddt + excel作为数据驱动模式的应用,使用HtmTetstRunner来生成测试用例。测试报告美化了界面,并且添加了截图
    但是,发现测试报告中,测试用例名称都是:test_login_[index]表示用例的编号,从1开始,递增。比如:test_login_01、test_login_02......test_login_0N

    • 希望能在不同的用例名称当中,显示自定义的用例名字。比如登陆用户名错误用例:测试报告中用例名称显示为test_login_name_err。密码错误的用例名称为:test_login_pwd_err

    将ddt.py源码,mk_test_name函数简单修改就可以了(这个函数是用来生成测试用例名字的),截图部分为修改前,标记出是需要修改的

    image.png

    修改后的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)
        # 测试用例名称读取 case_name 字段的值 start --Victor
        # 添加了对字典数据的处理。
        if not is_trivial(value) and type(value) is not dict:
            return "{0}_{1}".format(name, index)
        # 如果数据是字典,则获取字典当中的api_name对应的值,加到测试用例名称中。
        if type(value) is dict:
            try:
                value = value["case_name"]  # case_name作为value值
            except:
                return "{0}_{1}".format(name, index)
        # if not is_trivial(value):  # 注释原有方法
        #     return "{0}_{1}".format(name, index)
        # 测试用例名称读取 case_name 字段的值 end --Victor
        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()方法,然后运行测试用例就可以了(注意 使用ddt的时候需要包含字段case_name,否则名称还是下标递增)

    运行结果:


    读取case中case_name字段的值

    原文:博客园-简

    相关文章

      网友评论

          本文标题:ddt源码修改:HtmlTestRunner报告用例名称读取ca

          本文链接:https://www.haomeiwen.com/subject/khsyhqtx.html