junit-xml-2
junit-xml-2 (version 1.9) is a Python library designed for creating JUnit XML test result documents. It is a fork of `https://github.com/kyrus/python-junit-xml`, primarily created to ensure a tarball was published to PyPI. The library currently appears to be in maintenance mode, with its last release in September 2020, while the original project it forked from has seen more recent updates.
Warnings
- gotcha junit-xml-2 is a fork with its last release (1.9) in September 2020. The upstream `junit-xml` (kyrus/python-junit-xml) has a more recent 1.9 release from January 2023. Users needing the latest bug fixes or features from the upstream should use `junit-xml` directly if possible, or be aware `junit-xml-2` might be out of sync.
- gotcha The JUnit XML format lacks an official, universally adhered-to specification, leading to variations in how different tools generate and interpret these files. This library implements a common schema based on observed usage and Jenkins' implementation, but full compatibility across all parsing tools is not guaranteed.
- gotcha The library automatically removes 'illegal or discouraged' Unicode characters from the generated XML output. This behavior, while preventing XML parsing errors, can lead to silent data loss if test output contains such characters.
- gotcha Merely placing error output in `<system-err>` tags within a `TestCase` might not register a test as a failure or error in the `TestSuite` summary. To ensure tests are correctly counted as failed or erroneous, explicitly use `TestCase.add_failure_info(message, output, error_type)` for failures or `TestCase.add_error_info(message, output, error_type)` for errors.
Install
-
pip install junit-xml-2
Imports
- TestSuite
from junit_xml import TestSuite
- TestCase
from junit_xml import TestCase
Quickstart
from junit_xml import TestSuite, TestCase
# Create a list of test cases
test_cases = [
TestCase('TestFoo', 'some.class.name', 123.345, 'I am stdout!', 'I am stderr!'),
TestCase('TestBar', 'another.class', 54.123)
]
# Create a test suite and add test cases
ts = TestSuite("MyTestSuite", test_cases)
# Add a failing test case
failing_test = TestCase('TestFailure', 'failing.class', 10.0)
failing_test.add_failure_info('Assertion failed: expected X got Y', 'Traceback...')
ts.add_testcase(failing_test)
# Add an error test case
error_test = TestCase('TestError', 'error.class', 7.5)
error_test.add_error_info('Runtime error: division by zero', 'Traceback...', 'ZeroDivisionError')
ts.add_testcase(error_test)
# Generate XML string (pretty printing is on by default)
xml_string = TestSuite.to_xml_string([ts])
print(xml_string)
# You can also write the XML to a file
# with open('output.xml', 'w') as f:
# TestSuite.to_file(f, [ts], prettyprint=True)