Generate Html Reports


We can generate the XML format reports when pytest runs at the command line but it is bit difficult  to understand the end user/stockholders . Visual reports are a much better way to consume test result information, especially for end user/stockholders. Adding the pytest-html plugin to your test project enables you to print pretty HTML reports with one simple command line option.

For generating HTML reports you should install pytest-html module to your project.

Exampels:

Create  a test file test_additions.py
import pytest
import pytest_html

def test_add():
   
assert 5+10==15
def test_add_odd():
   
assert 3+7==10
def test_add_even():
   
assert 4+6==11
def test_add_evenodd():
   
assert 5+4==10

You can make use of the following command to execute the same.

pytest test_addition.py -v --html=”C:\\Reports\reports_test_additions.html”

Below is the HTML report that contains information about the tests & their results 
pytest-html – Reports Enhancement

If you plan to add more details to the HTML report, you can do the same by creating extra list on the report object. Listed below are some of the options that can be used for adding more informative content in the pytest report.

Type
Example
Raw HTML
extra.html('<div>Additional HTML</div>')
extra.json({'name': 'pytest'})
Plain text
extra.text('Add some simple Text')
URL
extra.url('http://www.example.com/')
Image
extra.image(image, mime_type='image/gif', extension='gif')
Image
extra.image('/path/to/file.png')
Image
extra.image('http://some_image.png')


The following example adds the various types of extras using a pytest_runtest_makereport hook, which can be implemented in a plugin or conftest.py file:

Scenario:
Adding the screenshots in my pytest html test report for failed scenarios

Create a conftest.py file, below is the code
import pytest
import pytest_html
from selenium import webdriver

@pytest.fixture(scope='session')
def browser(request):
    base_url =
"http://automationpractice.com/"
   
chromePath = "C:\\executables\\chromedriver.exe"
   
driver = webdriver.Chrome(executable_path=chromePath)
    driver.get(base_url)
   
return driver

import datetime
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    x = datetime.datetime.now()
    timestamp = x.strftime(
"%d-%H-%M-%S") # add
   
pytest_html = item.config.pluginmanager.getplugin('html')
    outcome =
yield
   
report = outcome.get_result()
    extra =
getattr(report, 'extra', [])
   
if report.when == 'call':
       
#----------------------
       
feature_request = item.funcargs['request']
        driver = feature_request.getfixturevalue(
'browser')
        driver.save_screenshot(
'C:/TempFiles/scr' + timestamp + '.png')
       
#-----------------------------
        # always add url to report
       
extra.append(pytest_html.extras.text('some string', name='Different title'))
        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.png('C:/TempFiles/scr' + timestamp + '.png'))
            extra.append(pytest_html.extras.html(
'<div>Additional HTML</div>'))
        report.extra = extra

create a test file test_htmlreport.py
import time
def test_title(browser):
   
assert ("My Store1" == browser.title)

def tests_clickOnLoginLink(browser):

    browser.find_element_by_xpath(
"//a[@title='Log in to your customer account']").click()

def test_one(browser):
    time.sleep(
2)      browser.find_element_by_xpath("//input[@id='email']").send_keys("abc111@gmail.com")
    browser.find_element_by_xpath(
"//input[@name='passwd']").send_keys("abcd12341")
    browser.find_element_by_xpath(
"//button[@type='submit' and @id='SubmitLogin']").click()
    time.sleep(
3)
    errormsg= browser.find_element_by_xpath(
"//div[@class='alert alert-danger']//li").text
   
assert (errormsg=="Authentication failed.1")

execute the test using below command 
pytest test_htmlreport.py -v -html=”c:\Reports\Report_Test_file.html” 
output will be
It will add the Screenshots to pytest html report for the failed test cases.

Comments