JUnit to HTML Report Generator
junit2html is a lightweight, self-contained Python tool designed to convert JUnit XML test reports into human-readable, single HTML dashboards. It's an essential utility for visualizing test results, especially within CI/CD pipelines, by transforming complex XML data into a clean and responsive HTML file. The current version is 31.0.5, indicating active development with regular updates.
Warnings
- gotcha After `pip install junit2html`, the `junit2html` command might not be directly available in your system's PATH. If you encounter a 'command not found' error, try executing it as a Python module: `python -m junit2htmlreport JUNIT_XML_FILE [NEW_HTML_FILE]`.
- gotcha junit2html expects valid JUnit XML format. Input files with non-standard structures, incorrect encodings, or malformed XML (e.g., some outputs from Robot Framework without proper pre-processing) might lead to 'Could not find test suites in results xml' or other parsing errors.
- breaking Version 31.0.4 was yanked from PyPI due to a reported bug. Users who installed this specific version might experience issues. It is recommended to update to 31.0.5 or later.
Install
-
pip install junit2html
Imports
- CLI tool
from junit2htmlreport import main # for programmatic access to CLI internals (less common)
Quickstart
# Create a dummy JUnit XML file for demonstration
# In a real scenario, this would be generated by your test runner (e.g., pytest --junitxml=results.xml)
import os
dummy_xml_content = '''
<testsuites>
<testsuite name="MyTestSuite" tests="2" failures="1" errors="0" skipped="0" time="0.123">
<testcase classname="test_module.TestClass" name="test_passing_case" time="0.050"/>
<testcase classname="test_module.TestClass" name="test_failing_case" time="0.073">
<failure message="AssertionError: 1 != 2">Traceback (most recent call last):...</failure>
</testcase>
</testsuite>
</testsuites>
'''
with open('results.xml', 'w') as f:
f.write(dummy_xml_content)
# Run junit2html from the command line
# This is the primary usage pattern.
# The 'junit2html' command should be available in your PATH after installation.
# Alternatively, use 'python -m junit2htmlreport results.xml report.html'
import subprocess
try:
# Using the installed CLI command
subprocess.run(['junit2html', 'results.xml', 'report.html'], check=True)
print("HTML report 'report.html' generated successfully.")
except FileNotFoundError:
print("'junit2html' command not found. Trying 'python -m junit2htmlreport'.")
try:
# Fallback to module execution
subprocess.run(['python', '-m', 'junit2htmlreport', 'results.xml', 'report.html'], check=True)
print("HTML report 'report.html' generated successfully via module execution.")
except subprocess.CalledProcessError as e:
print(f"Error generating report: {e}")
except subprocess.CalledProcessError as e:
print(f"Error generating report: {e}")
# Clean up dummy XML (optional)
os.remove('results.xml')