unittest-based test runner with Ant/JUnit like XML reporting.
npx @tessl/cli install tessl/pypi-unittest-xml-reporting@3.2.0A 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.
pip install unittest-xml-reportingimport xmlrunnerMain components:
from xmlrunner import XMLTestRunnerFor XML building utilities:
from xmlrunner.builder import TestXMLBuilder, TestXMLContextFor command-line test program:
from xmlrunner.runner import XMLTestProgramimport 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
)unittest-xml-reporting follows a layered architecture:
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.
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): ...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): ...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): ...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): ...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...]Transformation utilities for ensuring XML report compatibility with various versions of Jenkins xUnit plugins, including schema validation and attribute filtering.
def transform(xml_data): ...# 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')