用于生成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第三方库


热门话题
java需要建议如何打印ArrayList的剩余容量   ApachePOI如何在java中生成多级(行和列)透视表   java如何关闭静态线程池?   日期查询使用_id,但不使用日期值MongoDB   java如何编写记录参数和返回值的拦截器?   java在安卓中使用哪种音频格式来存储录音   Java/Python在C/C++中访问的动态链接库?   来自文本文件的java hashmap字数   java为什么onClick不启动拖动侦听器,但长时间单击工作正常?[安卓]   Java字符串到字节数组,缺少特殊字符§   java Spring捕获所有响应类型   序列化Java枚举:从文件反序列化任意枚举   java将jar包装为。用于部署的应用程序(OSX)和(.exe)?   java如何使TestNG向testngcustomsuite添加参数。xml?   java动态更改操作栏中的项目标题   java为什么我的2D数组乘法输出不正确?   java我在打开和关闭键时出错   JavaEclipse:“保存操作”选项中的导入+导出操作   Eclipse无法打开,给了我一个很长的错误。[爪哇]   java Hibernate搜索使用字段点符号“a.b.c”时,我应该用@Facet注释哪些字段