码迷,mamicode.com
首页 > 其他好文 > 详细

pytest 失败截图

时间:2019-12-27 12:02:18      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:open   test   官方   color   man   内容   更改   href   creating   

pytest-html官方说明

地址 https://github.com/pytest-dev/pytest-html#creating-a-self-contained-report

技术图片

 

 

 官方文档表示,html的内容支持HTML,json,jpg,url等多种形式。还举例说明。注意下面标记的地方,是报告内容变更需要我们替换的

技术图片

@pytest.hookimpl(hookwrapper=True)
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
        # screen = _capture_screenshot()
        filename = os.path.join(rootpath, screen.png)  #获取截图文件位置
        extra.append(pytest_html.extras.png(filename))   #传入文件地址
        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


def _capture_screenshot():
    return driver.get_screenshot_as_file(screen.png)  # 截图并保存

运行  pytest --html=report.html,发现代码报错,

再看官方文档,发现给出了说明

技术图片

命令行更改运行方式: pytest --html=report.html --self-contained-html

发现运行成功,但是有warning

技术图片

 报告截图是有的

技术图片

 官方文档表明存入png格式为:extra.png(image),可能是因为我直接传的文件地址导致的问题

于是修改截图存入的数据,改为base64

@pytest.hookimpl(hookwrapper=True)
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
        screen = _capture_screenshot() # 修改后的代码
        # filename = os.path.join(rootpath, screen.png)
        extra.append(pytest_html.extras.png(screen)) # 修改后的代码
        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  


def _capture_screenshot():
    return driver.get_screenshot_as_base64() # 修改后的代码

运行  pytest --html=report.html --self-contained-html

技术图片

 有warning了,截图也成功

现在我们将截图的代码调整到失败判断中,只有失败的用例才需要截图

技术图片

 执行命令 pytest --html=report.html --self-contained-html

技术图片

只有失败的用例才会截图啦

最后源码为

技术图片
from selenium import webdriver
import pytest

driver = None

@pytest.hookimpl(hookwrapper=True)
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:
        xfail = hasattr(report, wasxfail)
        if (report.skipped and xfail) or (report.failed and not xfail):
            screen = _capture_screenshot()
            extra.append(pytest_html.extras.png(screen))
            # only add additional html on failure
            extra.append(pytest_html.extras.html(<div>Additional HTML</div>))
        report.extra = extra


def _capture_screenshot():
    return driver.get_screenshot_as_base64()

@pytest.fixture(scope=session, autouse=True)
def browser():
    global driver
    if driver is None:
        driver = webdriver.Firefox()
    return driver
View Code

pytest 失败截图

标签:open   test   官方   color   man   内容   更改   href   creating   

原文地址:https://www.cnblogs.com/jescs/p/12106238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!