JUnit to HTML Report Generator

31.0.5 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

The primary way to use junit2html is through its command-line interface. This quickstart demonstrates how to create a dummy JUnit XML file and then convert it into an HTML report using the `junit2html` command or its module-based execution.

# 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')

view raw JSON →