pytest html 增加列,pytest 框架修改 html 报告插入描述列和获取用例描述

unittest 框架是有实现读取用例的描述功能的,在运行的时候就可以显示出来,也可以在 html 报告中显示,规则是在 test_method 下面用 ‘’‘ ’‘’ 注释。

初用 pytset 框架直接运行我的 unittest 用例,生成了 html 报告,发现描述不见了,百度了一圈没有找到相关文章提及此事。

from datetime import datetime

from py.xml import html

import pytest

@user1hook

def pytest_html_results_table_header(cells):

cells.insert(2, html.th('Description'))

cells.insert(1, html.th('Time', class_='sortable time', col='time'))

cells.pop()

@user2hook

def pytest_html_results_table_row(report, cells):

cells.insert(2, html.td(report.description))

cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))

cells.pop()

@user3per

def pytest_runtest_makereport(item, call):

outcome = yield

report = outcome.get_result()

report.description = str(item.function.__doc__)

这里要修改的是获取描述的部分,在 pytest_runtest_makereport 接口里传入的 item,我的 unittest case 并没有找到这个 function 的变量,所以我打断点找了一下,发现是在 item._testcase._testMethodDoc 的变量里存放的用例描述,把它赋值给 report.description 即可。这里还有一点要注意,pytest_runtest_makereport 会多次被调用,setup 时,teardown 时,在 teardown 时调用,就没有_testMethodDoc 这个变量,这里会有空指针的错。所以我用 report.when 的值判断了一下:if report.when == 'call' or report.when == "setup":

效果图就不上了,就是 results 列表中多了一列 Discription。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值