Pytest-HTML 官方文档

一、官网资料
安装
要安装 pytest-html:
$ pip install pytest-html
然后运行你的测试:
$ pytest --html=report.html

 

创建自包含报表

为了遵守内容安全策略( CSP ) 插件,默认情况下,将分别存储CSS和图像等若干资产。 可以选择创建自包含报表,这在共享结果时更方便。 可以按以下方式执行这里操作:
$ pytest --html=report.html --self-contained-html
作为文件或者链接添加的图像将被链接为外部资源,这意味着独立报表html文件可能不会像预期那样显示这些图像。
插件将在添加文件或者链接到独立报表时发出警告。

增强报告

环境

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

 

额外内容

你可以在报表对象上创建'额外'列表,从而向HTML报告添加详细信息。 以下是可以添加的额外内容类型:
类型示例
 
原始 HTML
extra.html('<div>Additional HTML</div>')
extra.json({'name': 'pytest'})
纯文本
extra.text('Add some simple Text')
URL
图像
extra.image(image, mime_type='image/gif', extension='gif')
图像
extra.image('/path/to/file.png')
图像
extra.image('http://some_image.png')
 
注释:当从文件中添加图像时,路径可以是绝对的,也可以是 relative。
注意 : 使用 --self-contained-html 时,添加为文件或者链接的图像可能无法按预期工作,有关更多信息,请参见创建自包含的报表。
对于几种图像格式也有方便的类型:
图像格式示例
 
 
PNG
extra.png(image)
JPEG
extra.jpg(image)
SVG
extra.svg(image)
以下示例使用 pytest_runtest_makereport 钩子添加各种类型的附加项,可以在插件或者 conftest.py file: 中实现这些附加项
 1 @pytest.hookimpl(hookwrapper=True)
 2 def pytest_runtest_makereport(item, call):
 3     pytest_html = item.config.pluginmanager.getplugin('html')
 4     outcome = yield
 5     report = outcome.get_result()
 6     extra = getattr(report, 'extra', [])
 7     if report.when == 'call':
 8         # always add url to report
 9         extra.append(pytest_html.extras.url('http://www.baidu.com/'))
10         xfail = hasattr(report, 'wasxfail')
11         if (report.skipped and xfail) or (report.failed and not xfail):
12             # only add additional html on failure
13             extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
14         report.extra = extra

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

extra.append(pytest_html.extras.text('some string', name='Different title'))
 
二、Allure Report本地打开报错
 1.无法本地打开,可以通过pycharm打开,pycharm自带容器服务,开启一个端口运行。
 2.Anywhere运行
        Anywhere是一个随启随用的静态服务器,它可以随时随地将你的当前目录变成一个静态文件服务器的根目录。
        安装之后,这个运行最简单,在report目录下打开cmd,输入anywhere就可以启动一个服务
 
三、参考链接
posted @ 2020-08-05 11:57  昨日不可追  阅读(589)  评论(0编辑  收藏  举报