0
0
Selenium Pythontesting~5 mins

HTML report generation in Selenium Python

Choose your learning style9 modes available
Introduction

HTML report generation helps you see test results in a clear, colorful webpage. It makes understanding test outcomes easy and quick.

After running automated tests to share results with your team.
When you want a visual summary of passed and failed tests.
To keep a record of test runs for future reference.
When you need to show test results to non-technical people.
To quickly spot which tests need fixing.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
import HtmlTestRunner

class TestExample(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_google_title(self):
        self.driver.get('https://www.google.com')
        self.assertIn('Google', self.driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports'))

Use HtmlTestRunner to create HTML reports easily with unittest.

The output folder stores the generated HTML files.

Examples
This saves the HTML report in the folder named my_reports.
Selenium Python
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='my_reports'))
This sets a custom report file name TestReport.html.
Selenium Python
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports', report_name='TestReport'))
This combines all test results into one HTML report file.
Selenium Python
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports', combine_reports=True))
Sample Program

This test opens Google, checks the page title contains 'Google', then closes the browser. The test results are saved as an HTML report in the 'reports' folder.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
import HtmlTestRunner

class GoogleTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_title(self):
        self.driver.get('https://www.google.com')
        self.assertIn('Google', self.driver.title)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports'))
OutputSuccess
Important Notes

Make sure to install HtmlTestRunner with pip install html-testRunner before running tests.

Keep your test names clear; they appear in the report.

Open the generated HTML file in a browser to view the colorful test results.

Summary

HTML reports make test results easy to read and share.

Use HtmlTestRunner with unittest for simple HTML report generation.

Reports save in a folder you choose and show pass/fail details clearly.