Quick Guide to Generating Clear HTML Test Reports with unittestreport
This article introduces the open‑source unittestreport framework for Python, explains how to install it, configure report parameters, use multi‑threaded execution, enable email notifications, and extend it with HTMLTestRunner compatibility to produce customizable, visual HTML test reports for unittest and pytest projects.
In daily testing, publishing clear and readable test reports speeds up result comprehension; automated testing benefits from quickly generated visual HTML reports.
The popular third‑party libraries (e.g., HTMLTestRunner, Allure) each have pros and cons. This article presents a method to rapidly generate clear, visual HTML test reports using the unittestreport framework, which supports both unittest and pytest, offers multiple report styles, and provides practical, aesthetic, and shareable features.
unittestreport Overview
unittestreport is an open‑source extension library based on unittest, originally created to generate HTML reports. It now supports HTML report generation, data‑driven testing, failure reruns, multithreading, and result pushing.
Installation commands:
pip install unittestreport
pip install pytest-testreport
Quick Start
The framework provides a TestRunner class that replaces unittest's TextTestRunner; after test execution it automatically creates an HTML report.
Typical usage flow: load test cases → create test program → run tests → generate report.
Key TestRunner parameters (default values in brackets):
suite: test suite
filename: report file name (report.html)
report_dir: report storage path (./reports)
tester: tester name (测试员)
desc: report description (xx项目测试生成的报告)
title: report title (测试报告)
templates: template type (1‑3)
Three built‑in templates provide different visual styles, including default, historical data statistics, and BeautifulReport‑like layout.
High‑Frequency Features
Data‑driven testing separates test data from logic, improving reuse and maintenance. The unittestreport.dataDriver module uses ddt to support list, JSON, and YAML data sources, applied via @ddt, @list_data, @json_data, or @yaml_data decorators.
Multithreading & Failure Rerun
TestRunner.run() accepts parameters for concurrent execution and retry:
thread_count: number of threads
count: retry attempts for failed cases
interval: wait time between retries (seconds)
Threads are launched per test class, preserving order within a class while allowing parallel execution across classes.
Email Push
unittestreport uses SMTP to send reports via TestRunner.send_email(). It supports plain (port 25), SSL (port 465), and TLS (port 587) connections; the source was extended to handle TLS (port 587) errors.
Integration with HTMLTestRunner
To adopt HTMLTestRunner styles, the HTMLTestRunner.py file (Python 3 compatible) is copied into unittestreport's core directory, imported in __init__.py, and integrated into the main workflow. Adjustments were made to HTMLTestRunner's constructor and run() method to accept an optional suite parameter, ensuring compatibility with both frameworks.
Two calling patterns are demonstrated for generating HTMLTestRunner‑style reports using unittestreport.
Conclusion
The article shows how to quickly generate clear, visual HTML test reports with unittestreport, and details practical extensions such as multithreading, failure reruns, email notifications, and HTMLTestRunner compatibility.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.