Registry / testing / junit-xml-2

junit-xml-2

JSON →
library1.9pypypi✓ verified 22d ago

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.

pip install junit-xml-2
INSTALL
IMPORT
SIG · JUNIT-XML-2
J
junit-xml-2
testingpythonv1.9
Install
1.6s avg
Import
103ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.112s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.094s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TestSuite
from junit_xml import TestSuite
TestCase
from junit_xml import TestCase

This quickstart demonstrates how to create `TestCase` and `TestSuite` objects, add details like stdout/stderr, and correctly register test failures and errors. The generated XML can then be printed to the console or written to a file. This code is based on the usage pattern of the `junit-xml` library, which `junit-xml-2` is a fork of.

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)
Debug
Known issues
gotchajunit-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.
fix
Consider `pip install junit-xml` for the actively maintained version if newer features or fixes are required, or consult the `junit-xml-2` GitHub repository for specific fork changes.
affects: <=1.9
gotchaThe 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.
fix
Validate the generated XML against your specific CI/parsing tool's requirements. Review the `Common JUnit XML Format & Examples` for prevalent conventions.
affects: All
gotchaThe 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.
fix
Sanitize test output for non-standard Unicode characters before passing it to `TestCase` if preserving exact character fidelity is critical, or be aware of this automatic stripping.
affects: All
gotchaMerely 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.
fix
Always use `TestCase.add_failure_info()` or `TestCase.add_error_info()` methods when a test fails or encounters an error to ensure proper reporting.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'junit_xml'
The Python package installed is `junit-xml-2`, but the code attempts to import the module `junit_xml`. Although `junit-xml-2` is a fork of the `junit-xml` project, it exposes its functionality under the `junit_xml` module name.
fix
Ensure `junit-xml-2` is installed via `pip install junit-xml-2` (or `pip install junit-xml` if you intend to use the upstream project), and then correctly import using `from junit_xml import TestSuite, TestCase`.
JUnit XML report shows errors='0' failures='0' despite tests failing
The library does not automatically register a test as failed or erroneous simply by adding content to the `<system-err>` or `<system-out>` tags. Explicit methods must be used to mark a test case with a failure or error status.
fix
To correctly report failures or errors, use the `TestCase.add_failure_info(message, output, error_type)` or `TestCase.add_error_info(message, output, error_type)` methods on the `TestCase` object.
parser error : CData section not finished
The JUnit XML output generated by the library may contain illegal or discouraged Unicode characters within `CDATA` sections (e.g., `system-out`, `system-err`). While the library attempts to sanitize these, some characters can still lead to the XML being ill-formed, causing parsing errors in external CI tools or XML validators.
fix
Manually sanitize or encode test output for non-standard Unicode characters before passing it to `TestCase` elements. Alternatively, ensure the environment generating the test output uses an encoding compatible with XML 1.0 specifications.
Upgrade
Version history
1.9latest on PyPI · released Sep 30, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources