python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

一、html报告错误截图

这次介绍pytest第三方插件pytest-html

这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告

 

今天在这里介绍pytest生成的报告怎么带有截图,这在web自动化测试非常有用。

需求是测试用例错误就截图,方法如下:

我们要新建一个关于截图的插件文件conftest.py,注意,文件名不能变,因为pytest-html会自动找这个自己写的插件,内容如下:

from selenium import webdriver
import pytest
driver = None


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
    Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
    :param item:
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            _capture_screenshot(file_name)
            if file_name:
                html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                       'οnclick="window.open(this.src)" align="right"/></div>' % file_name
                extra.append(pytest_html.extras.html(html))
        report.extra = extra


def _capture_screenshot(name):
    driver.get_screenshot_as_file(name)


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

 

关于conftest.py文件怎么应用,可以查看文档:conftest.py how to put

接下来,就是写用例了,在与conftest当前文件夹下写用例文件test_aa.py,如下

def test_screenshot_on_test_failure(browser):
    browser.get("https://www.baidu.com")
    assert False

 

然后再次用pytest运行,运行方式如下:

E:\>pytest -s -v test_aa.py --html=report.html

然后我们可以在E盘下看到生成了report.html文件及测试用例为名的png截图文件

打开html文件,详情如下:

 参考:

https://pypi.python.org/pypi/pytest-html

二、失败重试

使用的插件是pytest-rerunfailures,官网这里

使用方法:

在测试时加入--rerun参数

py.test --rerun 2

用例失败再重测2次

 

转载于:https://www.cnblogs.com/landhu/p/7463631.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值