首页 > 其他分享 >unittest中使用ddt后生成的测试报告名称如何修改?(如test_api_0修改成test_api_0_titile)

unittest中使用ddt后生成的测试报告名称如何修改?(如test_api_0修改成test_api_0_titile)

时间:2023-01-10 11:07:57浏览次数:39  
标签:index name format value 修改 api test trivial

修改前:

Unittest使用ddt后生成的测试报告用例名称为: 在这里插入图片描述 即就是,以“test_xx_数字”为格式的用例名称,感觉满足不了我们的测试需求,不够直观。那么怎么修改呢?

查看ddt源码

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)
    
  • 从方法mk_test_name中,我们看到该方法的描述是“Generate a new name for a test case.”,即就是为测试用例创建一个名称,那么改这个方法就行了
  • 方法中返回的是name和index,即"{0}_{1}".format(name, index)
  • 那么就明白了,我们改返回的内容就行了

修改后

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) and type(value) is not dict: # 增加的地方,增加value的字典判断

        return "{0}_{1}_{2}".format(name, index, value.name) # 修改的地方,增加返回的值
    if type(value) is dict: # 增加的地方
        try: # 增加的地方
            value = value["name"] + "_" + value["function"] # 增加的地方,name和function必须是execl用例中整正存在的表头,这里我是把两个表头合并了(name是我表格中接口的名称,function是表格中接口的功能描述)
        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)

在这里插入图片描述 在这里插入图片描述

标签:index,name,format,value,修改,api,test,trivial
From: https://blog.51cto.com/NoamaNelson/5999375

相关文章

  • windows系统下的mysql 8.0 修改密码、创建用户
    https://blog.csdn.net/qq_40015409/article/details/1115832351.以管理员权限运行cmd2.停止服务netstopmysql//不需要关闭服务3.cdmysql安装目录4.mysqld--cons......
  • OpenWRT 修改固件默认显示中文
    固件默认中文基本上网上的多数都是安装之后的处理,少数是编译固件的,但是试过都没起作用,改lua的源码都不行(没仔细研究,试着改了下luci/modules/luci-base/luasrc/i18n.lua下l......
  • YOLO-Z | 记录修改YOLOv5以适应小目标检测的实验过程
    https://mp.weixin.qq.com/s/Dees5BvTeBt37fP4OpSQGwhttps://www.cnblogs.com/shuimuqingyang/p/15752821.html随着自动驾驶汽车和自动赛车越来越受欢迎,对更快、更准确......
  • stack 修改列名
    importpandasaspdimportnumpyasnpdata=pd.read_excel('/Users/xujingfei/Desktop/成都补货数据.xlsx',engine='openpyxl')data['岳阳库存'].fillna(0,inplace=......
  • Lambda 表达式与 Stream API
    初创建于:2022-07-2707:48Lambda表达式Lambda表达式允许把一个函数作为一个方法的参数,lambda表达式的语法格式如下:{params}->expression//或{params}......
  • contest739E. Gosha is hunting 题解报告
    题目地址题意:现在一共有\(n\)只神奇宝贝。你有\(a\)个『宝贝球』和\(b\)个『超级球』。『宝贝球』抓到第\(i\)只神奇宝贝的概率是\(p_i\),『超级球』抓到的......
  • PGSQL修改在使用中的数据库名称
    SELECTpg_terminate_backend(pg_stat_activity.pid)FROMpg_stat_activityWHEREdatname='hot'ANDpid<>pg_backend_pid();alterdatabasehotrenametohot_predict;......
  • shell的test命令
    目录shell的test命令1、数值测试2、字符串测试3、文件测试shell的test命令shell的test命令用于检查某个条件是否成立,可以通过数值、字符串、文件三方面进行测试。1、数......
  • 微信支付APIV3私钥与证书配置
    1.加载商户私钥(privateKey:私钥字符串)这个私钥是下载证书的的:apiclient_key.pem  2.转换下单时的证书文档:https://github.com/wechatpay-apiv3/CertificateDownloa......
  • 火山引擎 DataTester:5 个优化思路,构建高性能 A/B 实验平台
    导读:DataTester是由火山引擎推出的A/B测试平台,覆盖推荐、广告、搜索、UI、产品功能等业务应用场景,提供从A/B实验设计、实验创建、指标计算、统计分析到最终评估上线......