or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-runner.mddjango-integration.mdindex.mdjenkins-compatibility.mdresult-collection.mdxml-utilities.md
tile.json

tessl/pypi-unittest-xml-reporting

unittest-based test runner with Ant/JUnit like XML reporting.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/unittest-xml-reporting@3.2.x

To install, run

npx @tessl/cli install tessl/pypi-unittest-xml-reporting@3.2.0

index.mddocs/

unittest-xml-reporting

A unittest-based test runner that extends Python's built-in unittest framework to generate XML test reports in xUnit format. Compatible with build systems, IDEs, and continuous integration servers like Jenkins, this library provides comprehensive test result reporting while maintaining complete compatibility with standard unittest.TestCase workflows.

Package Information

  • Package Name: unittest-xml-reporting
  • Language: Python
  • Installation: pip install unittest-xml-reporting
  • Dependencies: None (core functionality), lxml (for extra features)
  • Python Version: 3.7+

Core Imports

import xmlrunner

Main components:

from xmlrunner import XMLTestRunner

For XML building utilities:

from xmlrunner.builder import TestXMLBuilder, TestXMLContext

For command-line test program:

from xmlrunner.runner import XMLTestProgram

Basic Usage

import unittest
import xmlrunner

class TestExample(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(2 + 2, 4)
    
    def test_subtraction(self):
        self.assertEqual(5 - 3, 2)

if __name__ == '__main__':
    # Generate XML reports in test-reports directory
    unittest.main(
        testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
        failfast=False, buffer=False, catchbreak=False
    )

Architecture

unittest-xml-reporting follows a layered architecture:

  • XMLTestRunner: Main test runner that orchestrates test execution and report generation
  • _XMLTestResult: Captures test results and generates XML reports using the JUnit format
  • TestXMLBuilder/TestXMLContext: Low-level XML document construction utilities
  • Django Integration: Specialized test runner for Django projects
  • Jenkins Compatibility: Transform utilities for various CI/CD systems

The library maintains full compatibility with Python's unittest framework while adding XML reporting capabilities, making it a drop-in replacement for TextTestRunner in CI/CD environments.

Capabilities

XML Test Runner

Main test runner class that executes tests and generates XML reports in JUnit format. Supports both directory-based and single-file output with configurable report formatting.

class XMLTestRunner:
    def __init__(self, output='.', outsuffix=None, elapsed_times=True, 
                 encoding='UTF-8', resultclass=None, **kwargs): ...
    def run(self, test): ...

Core Test Runner

Result Collection and XML Generation

Comprehensive test result collection system that captures test outcomes, timing information, stdout/stderr, and generates properly formatted XML reports compatible with CI/CD systems.

class _XMLTestResult:
    def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1,
                 elapsed_times=True, properties=None, infoclass=None): ...
    def generate_reports(self, test_runner): ...

Result Collection

XML Document Construction

Low-level utilities for building XML test reports with proper JUnit schema compliance, including context management, counter tracking, and CDATA section handling.

class TestXMLBuilder:
    def __init__(self): ...
    def begin_context(self, tag, name): ...
    def append(self, tag, content, **kwargs): ...
    def finish(self): ...

XML Utilities

Django Framework Integration

Specialized test runner for Django projects that integrates unittest-xml-reporting with Django's testing framework, supporting Django-specific configuration options.

class XMLTestRunner(DiscoverRunner):
    def get_test_runner_kwargs(self): ...
    def run_suite(self, suite, **kwargs): ...

Django Integration

Command-Line Interface

Direct command-line execution for running tests with XML reporting. Supports test discovery, module execution, and configurable output options.

class XMLTestProgram(TestProgram):
    def __init__(self, *args, **kwargs): ...
    def runTests(self): ...

Command-line usage:

python -m xmlrunner [discover] [options] [test_modules...]

Jenkins Compatibility Tools

Transformation utilities for ensuring XML report compatibility with various versions of Jenkins xUnit plugins, including schema validation and attribute filtering.

def transform(xml_data): ...

Jenkins Compatibility

Types

# Constants
UTF8: str = 'UTF-8'  # Default encoding for XML output

# Test outcome constants (from _TestInfo)
SUCCESS: int = 0
FAILURE: int = 1
ERROR: int = 2
SKIP: int = 3

# Test information container
class _TestInfo:
    def __init__(self, test_result, test_method, outcome=SUCCESS, 
                 err=None, subTest=None, filename=None, lineno=None, doc=None): ...
    
    test_result: _XMLTestResult
    outcome: int
    elapsed_time: float
    timestamp: str
    test_name: str
    test_id: str
    test_description: str
    test_exception_name: str
    test_exception_message: str
    test_exception_info: str
    stdout: str
    stderr: str
    filename: str | None
    lineno: int | None
    doc: str | None

# XML context for report generation
class TestXMLContext:
    def __init__(self, xml_doc, parent_context=None): ...
    
    xml_doc: Document
    parent: TestXMLContext | None
    counters: dict[str, int]
    element: Element
    _allowed_counters: tuple[str, ...] = ('tests', 'errors', 'failures', 'skipped')