用于生成html报告的pytest插件
pytest-html的Python项目详细描述
pytest html是pytest的插件,它生成 测试结果的HTML报告。
要求
要使用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")])
额外内容
您可以通过在 报表对象。以下是可以添加的额外内容类型:
Type | Example |
---|---|
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 format | Example |
---|---|
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
自定义此行为。
发布新版本
按照以下步骤发布项目的新版本:
- 用上游主机(git pull --rebase upstream master)更新本地主机
- 创建新分支并用新版本、今天的日期和所有更改/新功能更新CHANGES.rst。
- 提交并推送新分支,然后创建新的拉取请求
- 等待测试和检查,然后合并分支
- 合并后,再次更新本地主控形状(git pull --rebase upstream master)
- 用新版本(git tag v<new tag>)标记发行版
- 按下标签(git push upstream --tags)
- 完成。您可以监视Travis