Python pytest Test Framework Introduction four----pytest-html plugin HTML with error screenshot and failure re-test mechanism

Source: Internet
Author: User

One, HTML report error

This introduction pytest third-party plug-ins pytest-html

Here does not introduce how to use, because how to use the Internet has a lot of, here to give you a reference to the address, pytest-html generated HTML report

Here's how the pytest generated report is presented here today, which is useful in Web automation testing.

The requirement is a test case error, as follows:

We want to create a new plugin file about the conftest.py, note that the file name can not be changed, because pytest-html will automatically find this self-written plugin, the content is as follows:

 fromSeleniumImportWebdriverImportPytestdriver=None@pytest.mark.hookwrapperdefPytest_runtest_makereport (item):"""Extends the Pytest Plugin to take and embed screenshot in HTML report, whenever test fails. :p Aram Item:"""pytest_html= Item.config.pluginmanager.getplugin ('HTML') Outcome=yield Report=Outcome.get_result () extra= GetAttr (Report,'Extra', [])    ifReport.when = ='Pager' orReport.when = ="Setup": Xfail= hasattr (Report,'Wasxfail')        if(report.skipped andXfail)or(report.failed and  notxfail): file_name= Report.nodeid.replace ("::","_")+". PNG"_capture_screenshot (file_name)iffile_name:html='<div>'                        'onclick= "window.open (THIS.SRC)" align= "right"/></div>'%file_name extra.append (pytest_html.extras.html (HTML)) Report.extra=Extradef_capture_screenshot (name): Driver.get_screenshot_as_file (name) @pytest. Fixture (Scope='Session', autouse=True)defbrowser ():GlobalDriverifDriver isNone:driver=Webdriver. Firefox ()returnDriver

For information on how the conftest.py file is applied, you can view the document: conftest.py how to put

Next, is to write the use case, in the current folder with Conftest write use case file test_aa.py, as follows

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

Then run again with Pytest, running as follows:

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

Then we can see the PNG file with the name of the report.html file and the test case under the E disk

Open the HTML file with the following details:

Reference:

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

Second, failed retry

The plugin used is pytest-rerunfailures, the official website here

How to use:

Adding the--rerun parameter when testing

Py.test--rerun 2 use case failed and re-tested 2 times

Python pytest test Framework Introduction to the four-----pytest-html plug-in HTML with error and failure re-testing mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.