如何在 python pytest html 报告中包含屏幕截图 [英] How do I include screenshot in python pytest html report
问题描述
当我在浏览器中点击以下网址时https://192.168.xx.xxx/Test/屏幕截图"我在浏览器中得到了被测设备屏幕的截图.
When I hit the below url in browser "https://192.168.xx.xxx/Test/ScreenCapture" I get the screenshot of the device screen under test in the browser.
如何在我的 pytest html 测试报告中添加屏幕截图.目前我正在使用下面的代码,它在指定的测试目录中捕获屏幕截图.
How do I add the screenshot in my pytest html test report. Currently I am using below code which captures screen shot in the test directory specified.
url = 'https://192.168.xx.xxx/Test/ScreenCapture'
driver.get(url) driver.save_screenshot('/home/tests/screen.png')
我正在使用以下命令运行我的 pytest:py.test --html=report.html --self-contained-html screentest.py
I am running my pytest with below command : py.test --html=report.html --self-contained-html screentest.py
推荐答案
来自文档 https://pypi.org/project/pytest-html/:您可以通过创建额外"
From the documentation https://pypi.org/project/pytest-html/: You can add details to the HTML reports by creating an ‘extra’
extra.image(image, mime_type='image/gif', extension='gif')
你需要做一个钩子.再次来自文档:
You need to make a hook. Again from doc:
import pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call':
# always add url to report
extra.append(pytest_html.extras.url('http://www.example.com/'))
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
report.extra = extra
这篇关于如何在 python pytest html 报告中包含屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!