前言

pytest-html测试报告默认是不展示用例描述Description内容,之前用unittest生成的报告是可以展示用例的描述,也就是test开头的用例下三个引号里面的注释(docstring)内容。

pytest-html框架是可以修改生成的报告内容的,可以自己添加和删除html报告的table内容。

修改报告

pytest-html官方文档地址【https://pypi.org/project/pytest-html/】

l可以通过为标题行实现自定义钩子来修改列,下面的示例在conftest.py脚本中使用测试函数docstring添加描述(Description)列,添加可排序时间(Time)列,并删除链接(Link)列:

from datetime import datetime
from py.xml import html
import pytest @pytest.mark.optionalhook
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() @pytest.mark.optionalhook
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() @pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
report.description = str(item.function.__doc__)

还可以通过pytest_html_results_table_row 挂钩删除所有单元格来删除结果。下面的示例从报表中删除所有测试通过的结果:

import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
if report.passed:
del cells[:]

日志输出和附加HTML可以通过pytest_html_results_table_html挂钩来修改。下面的示例清空测试通过的日志输出:

import pytest

@pytest.mark.optionalhook
def pytest_html_results_table_html(report, data):
if report.passed:
del data[:]
data.append(html.div('No log output captured.', class_='empty log'))

添加Description

通过上面的官方文档,可以自己修改下测试报告,在报告里面添加一列的内容,添加到第二列,于是修改如下,红色代码全部注释掉

第三个@pytest.mark.hookwrapper,这个在之前测试报告里面添加截图时候,已经写过了,只需在最后加一句代码即可

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

代码参考

from datetime import datetime
from py.xml import html
import pytest @pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
当测试失败的时候,自动截图,展示到html报告中
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', []) if report.when == 'call' or report.when == "setup":
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
file_name = report.nodeid.replace("::", "_")+".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra
report.description = str(item.function.__doc__) @pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
cells.insert(1, html.th('Description')) @pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
cells.insert(1, html.td(report.description))

效果展示

修改完之后cmd运行

pytest --html=report.html --self-contained-html

---------------------------------pytest结合selenium自动化完整版-------------------------

全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

作者:上海-悠悠 QQ交流群:874033608

也可以关注下我的个人公众号:yoyoketang

pytest文档20-pytest-html报告优化(添加Description)的更多相关文章

  1. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  2. pytest文档13-allure2生成html报告(史上最详细)

    前言 allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1. ...

  3. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  4. pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)

    前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...

  5. pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  6. pytest文档55-plugins插件开发

    前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...

  7. pytest文档43-元数据使用(pytest-metadata)

    前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...

  8. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  9. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

随机推荐

  1. Z+F激光扫描仪

    链接:https://zhuanlan.zhihu.com/p/48589754 三维扫描仪有三个误差来源: ● 线性误差(激光雷达部分/LARA) ● 测距噪声(激光雷达部分/LARA) ● 测角误 ...

  2. 开发第一个maven示例

    mavenDemo目录如下: 在webapp下新建index.jsp文件 浏览器访问:http://localhost:8080/mavenDemo/index.jsp

  3. MongoDB 副本集的常用操作及原理

    本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操 ...

  4. BZOJ 2839: 集合计数 广义容斥

    在一个 $N$ 个元素集合中的所有子集中选择若干个,且交集大小为 $k$ 的方案数. 按照之前的套路,令 $f[k]$ 表示钦定交集大小为 $k$,其余随便选的方案数. 令 $g[k]$ 表示交集恰好 ...

  5. fgetc,getc,fputc,putc,putchar,getchar

    转自 http://blog.csdn.net/todd911/article/details/8952565 输入输出函数家族 家族名                   目的           ...

  6. redis配置数据持久化---APPEND ONLY MODE

    Redis配置数据持久化---APPEND ONLY MODE 2016年04月01日 19:05:11 阅读数:9918 Redis可以实现数据的持久化存储,即将数据保存到磁盘上. Redis的持久 ...

  7. 将List<E>内对象按照某个字段排序

    主要用到java.util的Collections类 Collections.sort(list); 其中,E必须实现Comparable<E>接口

  8. 下载MAMP

    下载https://www.mamp.info/en/downloads/ MAMP PRO will create copies of the MySQL databases located in ...

  9. java.lang.IllegalStateException: Method get not annotated with HTTP method type (ex. GET, POST);

    明明指定了请求方法类型还报错: 代码: @RequestMapping(value="/enterprise/detail",method = RequestMethod.POST ...

  10. GIAC 技术大会 Redis 演讲文字稿

    附录:https://mp.weixin.qq.com/s/mvAkPXBayAzT_RWFdsOt5A 观众朋友们,我是来自掌阅的工程师钱文品,今天我带来的是分享主题是:Redis 在海量数据和高并 ...