unittest-based test runner with Ant/JUnit like XML reporting.
Main test runner classes that execute tests and orchestrate XML report generation. Provides both programmatic and command-line interfaces for running tests with XML output.
Primary test runner that extends unittest's TextTestRunner to generate XML reports in JUnit format. Handles test execution, result collection, and XML report generation.
class XMLTestRunner(TextTestRunner):
def __init__(self, output='.', outsuffix=None, elapsed_times=True,
encoding='UTF-8', resultclass=None, **kwargs):
"""
Initialize XML test runner.
Parameters:
- output: str or file-like object, output directory or file stream
- outsuffix: str or None, suffix for output files (default: timestamp)
- elapsed_times: bool, include timing information in reports
- encoding: str, XML encoding (default: 'UTF-8')
- resultclass: class or None, custom result class
- **kwargs: additional arguments passed to TextTestRunner
"""
def run(self, test):
"""
Run test case or test suite and generate XML reports.
Parameters:
- test: TestCase or TestSuite instance
Returns:
- TestResult: result object containing test outcomes
"""Directory Output (Multiple Files)
import unittest
import xmlrunner
# Generate separate XML file for each test class
runner = xmlrunner.XMLTestRunner(
output='test-reports',
outsuffix='20231201',
elapsed_times=True
)
unittest.main(testRunner=runner)Single File Output
import xmlrunner
# Generate single XML report file
with open('test-results.xml', 'wb') as output:
runner = xmlrunner.XMLTestRunner(
output=output,
elapsed_times=True
)
unittest.main(testRunner=runner)In-Memory Output
import io
import xmlrunner
# Generate XML report in memory
output = io.BytesIO()
runner = xmlrunner.XMLTestRunner(output=output)
unittest.main(testRunner=runner, exit=False)
xml_content = output.getvalue()
print(xml_content.decode('utf-8'))Command-line test program with XML reporting capabilities. Extends unittest's TestProgram to add XML-specific command-line options.
class XMLTestProgram(TestProgram):
def __init__(self, *args, **kwargs):
"""
Initialize XML test program with command-line argument parsing.
Command-line options:
- -o DIR, --output DIR: directory for XML reports
- --output-file FILENAME: single XML report file
- --outsuffix STRING: custom output suffix
Standard unittest options also supported.
"""
def runTests(self):
"""Execute tests with XML reporting based on command-line arguments."""Command-Line Usage
# Run tests with XML output to directory
python -m xmlrunner discover -t tests -o /tmp/reports
# Run specific test module
python -m xmlrunner tests.test_module -o test-reports
# Generate single output file
python -m xmlrunner --output-file results.xml tests
# Custom suffix for multiple files
python -m xmlrunner --outsuffix build-123 testsProgrammatic Usage
import sys
from xmlrunner.runner import XMLTestProgram
# Simulate command-line execution
sys.argv = ['xmlrunner', 'discover', '-t', 'tests', '-o', 'reports']
XMLTestProgram(module=None)# Output configuration
output: str | file_like = '.' # Output directory or file stream
outsuffix: str | None = None # File suffix (None = timestamp, '' = no suffix)
encoding: str = 'UTF-8' # XML encoding
# Test execution options
elapsed_times: bool = True # Include timing information
verbosity: int = 1 # Output verbosity level
failfast: bool = False # Stop on first failure
buffer: bool = False # Buffer stdout/stderr during tests
# Result customization
resultclass: type | None = None # Custom result classThe XMLTestRunner can be used as a drop-in replacement for unittest's default TextTestRunner:
import unittest
import xmlrunner
class MyTestCase(unittest.TestCase):
def test_example(self):
self.assertTrue(True)
if __name__ == '__main__':
unittest.main(
testRunner=xmlrunner.XMLTestRunner(output='reports'),
failfast=False,
buffer=False,
catchbreak=False
)The runner handles various error conditions gracefully:
Install with Tessl CLI
npx tessl i tessl/pypi-unittest-xml-reporting