前言

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)列:

  1. from datetime import datetime
  2. from py.xml import html
  3. import pytest
  4. @pytest.mark.optionalhook
  5. def pytest_html_results_table_header(cells):
  6. cells.insert(2, html.th('Description'))
  7. cells.insert(1, html.th('Time', class_='sortable time', col='time'))
  8. cells.pop()
  9. @pytest.mark.optionalhook
  10. def pytest_html_results_table_row(report, cells):
  11. cells.insert(2, html.td(report.description))
  12. cells.insert(1, html.td(datetime.utcnow(), class_='col-time'))
  13. cells.pop()
  14. @pytest.mark.hookwrapper
  15. def pytest_runtest_makereport(item, call):
  16. outcome = yield
  17. report = outcome.get_result()
  18. report.description = str(item.function.__doc__)

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

  1. import pytest
  2. @pytest.mark.optionalhook
  3. def pytest_html_results_table_row(report, cells):
  4. if report.passed:
  5. del cells[:]

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

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

添加Description

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

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

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

代码参考

  1. from datetime import datetime
  2. from py.xml import html
  3. import pytest
  4. @pytest.mark.hookwrapper
  5. def pytest_runtest_makereport(item):
  6. """
  7. 当测试失败的时候,自动截图,展示到html报告中
  8. :param item:
  9. """
  10. pytest_html = item.config.pluginmanager.getplugin('html')
  11. outcome = yield
  12. report = outcome.get_result()
  13. extra = getattr(report, 'extra', [])
  14. if report.when == 'call' or report.when == "setup":
  15. xfail = hasattr(report, 'wasxfail')
  16. if (report.skipped and xfail) or (report.failed and not xfail):
  17. file_name = report.nodeid.replace("::", "_")+".png"
  18. screen_img = _capture_screenshot()
  19. if file_name:
  20. html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
  21. 'onclick="window.open(this.src)" align="right"/></div>' % screen_img
  22. extra.append(pytest_html.extras.html(html))
  23. report.extra = extra
  24. report.description = str(item.function.__doc__)
  25. @pytest.mark.optionalhook
  26. def pytest_html_results_table_header(cells):
  27. cells.insert(1, html.th('Description'))
  28. @pytest.mark.optionalhook
  29. def pytest_html_results_table_row(report, cells):
  30. 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. CF436E Cardboard Box(贪心)

    题意 有nnn个关卡,第iii关可以花费aia_iai​的代价打一颗星,bib_ibi​的代价打两颗星.保证1≤ai<bi≤1091\le a_i<b_i\le10^91≤ai​<b ...

  2. docker学习(四)

    一.Docker数据管理 在容器中管理数据主要有两种方式:1.数据卷(Data volumes)2.数据卷容器(Data volume containers) 1.数据卷数据卷是一个可供一个或多个容器 ...

  3. iptables 相关命令

    1. 清除已有iptables规则 iptables -F iptables -X iptables -Z 2. 开放指定的端口(添加规则) iptables -A INPUT -s 127.0.0. ...

  4. NodeJS事件环

    1. 执行顺序说明 1. 清空主执行栈 2. 清空微任务队列 3. 运行一个timer队列的回调函数,询问微任务队列,如果有回调函数,清空. 4. 循环第3步,直到清空timer队列 5. 进入pol ...

  5. VsCode安装Go的相关插件

    今天在学习Go的时候,安装Go的相关插件,显示安装不上,但是右下角也一直会提示让你安装,当然你可以设置成忽略,为了开发效率,我选择了安装.然后出现了问题,一直Failed.在网上看到了很多的文章,不是 ...

  6. 微信小程序环境下将文件上传到 OSS

    步骤 1: 配置 Bucket 跨域 客户端进行表单直传到 OSS 时,会从浏览器向 OSS 发送带有 Origin 的请求消息.OSS 对带有 Origin 头的请求消息会进行跨域规则(CORS)的 ...

  7. 上传本地文件到github仓库

    第一步:新建仓库 给仓库一个名字,备注 得到仓库地址: https://github.com/Lucasli2018/java-1-mybatis.git 第二步:进入要上传的文件夹,初始化上传文件夹 ...

  8. ICEM-缺角正方体(2D转3D)

    原视频下载地址:https://yunpan.cn/cqKYEeSsQhJHt  访问密码 1cce

  9. GO 跟C++/C差异

    规范的语法(不需要符号表来解析) 垃圾回收(独有) 无头文件 明确的依赖 无循环依赖 常量只能是数字 int和int32是两种类型 字母大小写设置可见性(letter case sets visibi ...

  10. 7.linux磁盘管理 分区 建立文件系统 挂载使用

    一.磁盘管理 分区  建立文件系统  挂载使用   逻辑卷建立  磁盘阵列的建立  磁盘配额设定     fdisk -l       查看磁盘信息     df -Th        查看硬盘对应的 ...