unittest introduces pytest framework to realize abnormal screenshot and error rerun

Keywords: Junit Java snapshot

The unit test framework has been used to write test scripts. If you want to add the function of exception screenshot and automatic rerun, you have implemented it on junit in java before

The idea is to judge whether the result fails in tearDown, save the screenshot, or rewrite the rule, and try the place where it is executed

In unittest, it's also possible to add decorators or encapsulate assertions

If you add a decorator, you need to add a comment before the case method. If you encapsulate the assertion, you need to change the code of the case layer

Baidu's pytest is still reliable

pytest supports case running of unittest framework

Download a pytest rerun failures plug-in, run the command with the parameter: - reruns 3, then

Export HTML report, Download pytest HTML plug-in, add parameters: -- html=report.html, OK

The exception screenshot implements a confist.py. pytest will load the file itself, and the generated html report will have a link to the screenshot

In this way, you don't need to build your own wheel, and the script you wrote doesn't need to be modified at all

@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', [])

    # path = os.path.dirname(item.fspath)
    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;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % file_name
                extra.append(pytest_html.extras.html(html))
        report.extra = extra


def _capture_screenshot(filepath):
    DEVICE.snapshot(filepath)#Realize screenshot here

 

Posted by saish on Fri, 03 Jan 2020 15:47:59 -0800