junit-xml-2

1.9 · maintenance · verified Mon Apr 13

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

Install

Imports

Quickstart

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)

view raw JSON →