Generating Test Reports in Selenium Python
Track, Analyze, and Share Your Automation Test Results Effectively
Selenium is one of the most popular tools for automating browser-based testing. However, writing test cases is only half the battle. In real-world testing scenarios, generating detailed test reports is essential to understand the test coverage, identify failures, and share results with teams and stakeholders. When using Selenium with Python, you can leverage multiple libraries and frameworks to generate readable and actionable test reports.
In this blog, we’ll explore various methods to generate test reports in Selenium Python, covering tools like unittest, pytest, and HTMLTestRunner.
Why Test Reports Matter
Test reports are crucial for:
Tracking test case results (pass, fail, skipped)
Debugging failed scenarios
Measuring test coverage
Automating test feedback in CI/CD pipelines
Sharing results with QA, Dev, and business teams
Instead of reading console logs, reports provide structured summaries and easy-to-navigate details of test execution.
1. Using unittest with HTMLTestRunner
Python’s built-in unittest framework supports basic test reporting, and you can extend it with HTMLTestRunner to generate HTML reports.
Installation:
bash
Copy
Edit
pip install html-testRunner
Sample Code:
python
Copy
Edit
import unittest
from selenium import webdriver
import HtmlTestRunner
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_title(self):
self.driver.get("https://example.com")
self.assertIn("Example", self.driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main(
testRunner=HtmlTestRunner.HTMLTestRunner(output='reports')
)
After running, you'll find an HTML report inside the reports folder.
2. Using pytest with Allure Reporting
pytest is a more powerful and flexible testing framework. When combined with Allure, it can generate beautiful and interactive test reports.
Installation:
bash
Copy
Edit
pip install pytest allure-pytest
Writing a Test:
python
Copy
Edit
import pytest
from selenium import webdriver
def test_login():
driver = webdriver.Chrome()
driver.get("https://example.com")
assert "Example" in driver.title
driver.quit()
Generating Report:
bash
Copy
Edit
pytest --alluredir=allure-results
allure serve allure-results
This opens a web server with an interactive report dashboard showing steps, screenshots, logs, and more.
3. Adding Screenshots to Reports
Capturing screenshots on failure improves debugging. Here’s how to capture screenshots in a unittest test:
python
Copy
Edit
def tearDown(self):
if hasattr(self, '_outcome'):
for method, error in self._outcome.errors:
if error:
self.driver.save_screenshot("reports/error.png")
self.driver.quit()
Allure also supports screenshot attachment natively via decorators.
4. CI/CD Integration
You can integrate test report generation with CI/CD tools like Jenkins, GitHub Actions, or GitLab CI, so that:
Tests run on every code push
Reports are stored or emailed automatically
Failures are flagged before reaching production
Conclusion
Generating test reports in Selenium Python is a best practice that adds value to your automation framework. Whether you’re using unittest for small projects or pytest + Allure for enterprise-level testing, reports give you the visibility needed to maintain quality over time.
Learn Selenium with Pyhton Training Hyderabad
Read More: Automating Captcha with Selenium Python (Overview and Limits)
Visit IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment