$ pip install pytest-html
$ pytest --html=report.html
创建自包含报表
$ pytest --html=report.html --self-contained-html
增强报告
环境
defpytest_configure(config): config._metadata[‘foo‘] =‘bar‘
额外内容
原始 HTML
|
extra.html(‘<div>Additional HTML</div>‘)
|
extra.json({‘name‘: ‘pytest‘})
|
|
纯文本
|
extra.text(‘Add some simple Text‘)
|
URL
|
extra.url(‘http://www.example.com/‘)
|
图像
|
extra.image(image, mime_type=‘image/gif‘, extension=‘gif‘)
|
图像
|
extra.image(‘/path/to/file.png‘)
|
图像
|
extra.image(‘http://some_image.png‘)
|
|
|
PNG
|
extra.png(image)
|
JPEG
|
extra.jpg(image)
|
SVG
|
extra.svg(image)
|
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‘))
原文:https://www.cnblogs.com/for-master/p/13438903.html