ddt源码修改:HtmlTestRunner报告用例名称读取case中自定义名称

在网上查看了很多资料,搭建了一套测试框架用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函数简单修改就可以了(这个函数是用来生成测试用例名字的),截图部分为修改前,标记出是需要修改的

 

修改后的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,否则名称还是下标递增,如下)

# 获取测试数据,
def get_testcase(filepath, index, module):
    '''
    参考:https://www.2cto.com/kf/201805/745595.html
    :param filepath:  测试数据存放路径
    :param index:   Execl 里面sheet(工作表)的下标 从0开始
    :param module: 指定sheet中用例模块
    :return:
    '''
    try:
        file = xlrd.open_workbook(filepath)
        sheet = file.sheets()[index - 1]  # index 表单 默认一个Excel表中只存储一个表单  从0开始
        nrows = sheet.nrows  # 获取表单行数
        listdata = []
        for i in range(1, nrows):
            dict_canshu = {}
            dict_canshu['module'] = sheet.cell(i, 0).value  # 获取每行第一列内容
            if dict_canshu['module'] == module:  #
                dict_canshu['case_name'] = sheet.cell(i, 1).value  # 获取每行第二列内容,用做case的名字
                dict_canshu.update(eval(sheet.cell(i, 2).value))
                dict_canshu.update(eval(sheet.cell(i, 3).value))
                listdata.append(dict_canshu)
        logs.logger.info('获取%s内第%s个sheet(工作表)用例模块为"%s"的测试数据' % (filepath, index,module))
        logs.logger.info('测试数据listdata:%s' % listdata)
        return listdata
    except Exception as e:
        logs.logger.error('获取测试用例数据失败,原因:%s' % e)

运行结果:

 

读取case中case_name字段的值

原文:博客园-简

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值