用于生成html报告的pytest插件

pytest-html的Python项目详细描述


pytest html是pytest的插件,它生成 测试结果的HTML报告。

LicensePyPIConda ForgeTravisIssuesRequirements

要求

要使用pytest html,您需要以下先决条件:

  • python 2.7、3.6、pypy或pypy3

安装

要安装pytest html:

$ pip install pytest-html

然后使用以下命令运行测试:

$ pytest --html=report.html

ANSI代码

请注意,ANSI代码支持取决于 ansi2html包装。由于使用 对于权限较低的许可证,此包不作为依赖项包含。如果 您已经安装了此软件包,则ANSI代码将在中转换为HTML 你的报告。

创建独立的报告

为了尊重Content Security Policy (CSP), 默认情况下,一些资产(如css和图像)是单独存储的。 您也可以创建一个自包含的报表,它可以是 分享结果时很方便。这可以通过以下方式完成:

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

作为文件或链接添加的图像将作为外部资源链接, 这意味着独立的报表html文件不能显示这些图像 如预期。

插件将在向独立报表添加文件或链接时发出警告。

增强报告

外观

自定义css(层叠样式表)可以使用 --css选项。这些将按指定的顺序应用,并且可以 用于更改报表的外观。

$ pytest --html=report.html --css=highcontrast.css --css=accessible.css

环境

environment部分由pytest-metadata插件提供,可以访问 通过pytest_configure挂钩:

defpytest_configure(config):config._metadata['foo']='bar'

生成的表将按字母顺序排序,除非元数据是 collections.OrderedDict

附加摘要信息

您可以使用pytest_html_results_summary钩子编辑summary部分:

importpytestfrompy.xmlimporthtmldefpytest_html_results_summary(prefix,summary,postfix):prefix.extend([html.p("foo: bar")])

额外内容

您可以通过在 报表对象。以下是可以添加的额外内容类型:

TypeExample
Raw HTML^{tt1}$
JSON^{tt2}$
Plain text^{tt3}$
URL^{tt4}$
Image^{tt5}$
Image^{tt6}$
Image^{tt7}$

注意:从文件添加图像时,路径可以是绝对路径 或者是亲戚。

注意:使用--self-contained-html时,图像作为文件或链接添加 可能无法按预期工作,请参阅Creating a self-contained report部分 更多信息。

也有多种图像格式的方便类型:

Image formatExample
PNG^{tt9}$
JPEG^{tt10}$
SVG^{tt11}$

下面的示例使用 pytest_runtest_makereport钩子,可以在插件或 conftest.py文件:

importpytest@pytest.hookimpl(hookwrapper=True)defpytest_runtest_makereport(item,call):pytest_html=item.config.pluginmanager.getplugin('html')outcome=yieldreport=outcome.get_result()extra=getattr(report,'extra',[])ifreport.when=='call':# always add url to reportextra.append(pytest_html.extras.url('http://www.example.com/'))xfail=hasattr(report,'wasxfail')if(report.skippedandxfail)or(report.failedandnotxfail):# only add additional html on failureextra.append(pytest_html.extras.html('<div>Additional HTML</div>'))report.extra=extra

您还可以为除html之外的所有类型指定name参数,这将更改 已创建超链接:

extra.append(pytest_html.extras.text('some string',name='Different title'))

修改结果表

您可以通过实现头和 排。下面的示例conftest.py添加了一个描述列 测试函数docstring,添加可排序的时间列,并删除链接 列:

fromdatetimeimportdatetimefrompy.xmlimporthtmlimportpytestdefpytest_html_results_table_header(cells):cells.insert(2,html.th('Description'))cells.insert(1,html.th('Time',class_='sortable time',col='time'))cells.pop()defpytest_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.hookimpl(hookwrapper=True)defpytest_runtest_makereport(item,call):outcome=yieldreport=outcome.get_result()report.description=str(item.function.__doc__)

您还可以通过实现 pytest_html_results_table_row勾住并移除所有单元格。这个 以下示例将从报表中删除所有通过的结果:

importpytestdefpytest_html_results_table_row(report,cells):ifreport.passed:delcells[:]

可以通过实现 pytest_html_results_html钩子。以下示例将替换所有 附加的HTML和日志输出,并注意日志为空:

importpytestdefpytest_html_results_table_html(report,data):ifreport.passed:deldata[:]data.append(html.div('No log output captured.',class_='empty log'))

显示选项

默认情况下,results表中的所有行都将展开,但具有Passed的行除外。

可以使用查询参数?collapsed=Passed,XFailed,Skipped自定义此行为。

屏幕截图

Enhanced HTML report

贡献

分叉存储库并提交带有错误修复和增强的prs,贡献非常大欢迎。

可以使用tox在本地运行测试,例如执行python 2.7和3.6的测试execute:

tox -e py27,py36

发布新版本

按照以下步骤发布项目的新版本:

  1. 用上游主机(git pull --rebase upstream master)更新本地主机
  2. 创建新分支并用新版本、今天的日期和所有更改/新功能更新CHANGES.rst
  3. 提交并推送新分支,然后创建新的拉取请求
  4. 等待测试和检查,然后合并分支
  5. 合并后,再次更新本地主控形状(git pull --rebase upstream master
  6. 用新版本(git tag v<new tag>)标记发行版
  7. 按下标签(git push upstream --tags
  8. 完成。您可以监视Travis

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
爪哇太阳报。安全验证器。ValidatorException:PKIX路径生成失败   java理解为什么在onDispatchTouchEvent()返回True后仍调用onClick()   java如何在资源包中使用JSF标记/如何在资源包中重写URL?   java什么是流控制异常的替代方案?   java使用Spring数据JPA/MongoDB交叉存储,一个查询可以跨越两个数据库吗?   Apache Sling/felix中的java OSGi slf4j日志记录   为什么当我们想要水平/垂直旋转矩阵时,我们要把这个项除以2?   尝试从外部网页接收JSON字符串时出现安卓 Java NullPointerException   java执行异常:从Callable调用方法时   java在jetty上以调试模式运行webapp,使用maven jetty插件在intellij中构建成功(应用程序应在调试模式下启动)   带有内存数据库的linux Java应用程序的构建时间太长   基于java的随机数单元测试算法   java类型javax。摆动JComponent无法解析   Google日历API和UI小部件Java   java tomcat没有突然和任意地响应   java无法使用jsoup在html中获取图像src   我无法让Java接受键盘输入字符串   java如何开始使用Bambol而不让部署永远继续?   java如何使用另一个类的actionPerformed方法删除一个类/组件中的按钮?   java JSON反序列化brakets{}中的一系列对象