JUnitparser is a Python library designed to parse and manipulate JUnit/xUnit Result XML files. It allows users to read and modify existing XML reports or create new ones from scratch. The library is actively maintained, with its current version being 4.0.2, and has a consistent release cadence addressing new features and bug fixes.
pip install junitparserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a JUnit XML report from scratch, defining test cases with different results (skipped, successful, error), grouping them into a test suite, and then writing the complete report to an XML file. The `pretty=True` argument ensures human-readable formatting.
Update method calls to use new argument names (`file_or_filename`, `file`) and pass `pretty` as a keyword argument. Replace `to_console=True` with `obj.write(sys.stdout)`.
Always expect a `JUnitXml` object as the return type and iterate over its `testsuites` attribute if you need to access individual suites: `for suite in xml: ...`
Ensure your project is running on Python 3.6 or later.
Update code that accesses `TestCase.result` to expect and iterate over a list. For example, `case.result[0]` to get the first result or `for res in case.result: ...`.
Ensure that any value assigned to `TestCase.result` is an instance of a valid result class (e.g., `Success`, `Failure`, `Error`, `Skipped`) or a list of such instances.
Run `pip install junitparser` in your terminal to install the library.
Ensure you are iterating over a `JUnitXml` object to get `TestSuite` objects, and then iterating over `TestSuite` objects to get `TestCase` objects. If your XML has only one suite and no `<testsuites>` root, `JUnitXml.fromfile()` will parse it correctly, but direct iteration assumes a root of `TestSuite` or `JUnitXml` containing suites. For example: `xml = JUnitXml.fromfile('report.xml'); for suite in xml: for case in suite: pass`.Update your code to iterate over the `TestCase.result` list to access individual result objects and their attributes. For example, change `case.result.message` to `for r in case.result: print(r.message)`.
Provide a valid file path (string or `Path` object) or a file-like object when calling `write_xml()` or `JUnitXml.write()`. For example: `xml.write('output.xml')` or `write_xml(xml, 'output.xml')`.