Skip to main content

Manipulates JUnit/xUnit Result XML files

Project description

https://github.com/weiwei/junitparser/workflows/build/badge.svg?branch=master https://codecov.io/gh/weiwei/junitparser/branch/master/graph/badge.svg?token=UotlfRXNnK

junitparser handles JUnit/xUnit Result XML files. Use it to parse and manipulate existing Result XML files, or create new JUnit/xUnit result XMLs from scratch.

Features

  • Parse or modify existing JUnit/xUnit xml files.

  • Parse or modify non-standard or customized JUnit/xUnit xml files, by monkey patching existing element definitions.

  • Create JUnit/xUnit test results from scratch.

  • Merge test result xml files.

  • Specify xml parser. For example you can use lxml to speed things up.

  • Invoke from command line, or python -m junitparser

  • Python 2 and 3 support (As of Nov 2020, 1/4 of the users are still on Python 2, so there is no plan to drop Python 2 support)

Note on version 2

Version 2 improved support for pytest result xml files by fixing a few issues, notably that there could be multiple <Failure> or <Error> entries. There is a breaking change that TestCase.result is now a list instead of a single item. If you are using this attribute, please update your code accordingly.

Installation

pip install junitparser

Usage

You should be relatively familiar with the Junit XML format. If not, run pydoc on the exposed classes and functions to see how it’s structured.

Create Junit XML format reports from scratch

You have some test result data, and you want to convert them into junit.xml format.

from junitparser import TestCase, TestSuite, JUnitXml, Skipped, Error

# Create cases
case1 = TestCase('case1', 'class.name', 0.5) # params are optional
case1.classname = "modified.class.name" # specify or change case attrs
case1.result = [Skipped()] # You can have a list of results
case2 = TestCase('case2')
case2.result = [Error('Example error message', 'the_error_type')]

# Create suite and add cases
suite = TestSuite('suite1')
suite.add_property('build', '55')
suite.add_testcase(case1)
suite.add_testcase(case2)
suite.remove_testcase(case2)

# Add suite to JunitXml
xml = JUnitXml()
xml.add_testsuite(suite)
xml.write('junit.xml')

Read and manipulate exiting JUnit/xUnit XML files

You have some existing junit.xml files, and you want to modify the content.

from junitparser import JUnitXml

xml = JUnitXml.fromfile('/path/to/junit.xml')
for suite in xml:
    # handle suites
    for case in suite:
        # handle cases
xml.write() # Writes back to file

It is also possible to use a custom parser. For example lxml provides a plethora of parsing options. We can use them this way:

from lxml.etree import XMLParser, parse
from junitparser import JUnitXml

def parse_func(file_path):
    xml_parser = XMLParser(huge_tree=True)
    return parse(file_path, xml_parser)

xml = JUnitXml.fromfile('/path/to/junit.xml', parse_func)
# process xml...

Merge XML files

You have two or more XML files, and you want to merge them into one.

from junitparser import JUnitXml

xml1 = JUnitXml.fromfile('/path/to/junit1.xml')
xml2 = JUnitXml.fromfile('/path/to/junit2.xml')

newxml = xml1 + xml2
# Alternatively, merge in place
xml1 += xml2

Note that it won’t check for duplicate entries. You need to deal with them on your own.

Create XML with custom attributes

You want to use an attribute that is not supported by default.

from junitparser import TestCase, Attr, IntAttr, FloatAttr

# Add the custom attribute
TestCase.id = IntAttr('id')
TestCase.rate = FloatAttr('rate')
TestCase.custom = Attr('custom')
case = TestCase()
case.id = 123
case.rate = 0.95
case.custom = 'foobar'

Handling XML with custom element

There may be once in 1000 years you want to it this way, but anyways. Suppose you want to add element CustomElement to TestCase.

from junitparser import Element, Attr, TestSuite

# Create the new element by subclassing Element,
# and add custom attributes to it.
class CustomElement(Element):
    _tag = 'custom'
    foo = Attr()
    bar = Attr()

testcase = TestCase()
custom = CustomElement()
testcase.append(custom)
# To find a single sub-element:
testcase.child(CustomElement)
# To iterate over custom elements:
for custom in testcase.iterchildren(CustomElement):
    ... # Do things with custom element

Handling custom XML attributes

Say you have some data stored in the XML as custom attributes and you want to read them out:

from junitparser import TestCase, Attr, JUnitXml

# Create the new element by subclassing Element or one of its child class,
# and add custom attributes to it.
class MyTestCase(TestCase):
    foo = Attr()

xml = JUnitXml.fromfile('/path/to/junit.xml')
for suite in xml:
    # handle suites
    for case in suite:
        my_case = MyTestCase.fromelem(case)
        print(my_case.foo)

Command Line

$ junitparser --help
usage: junitparser [-h] [-v] {merge} ...

Junitparser CLI helper.

positional arguments:
{merge}        command
  merge        Merge Junit XML format reports with junitparser.

optional arguments:
-h, --help     show this help message and exit
-v, --version  show program's version number and exit
$ junitparser merge --help
usage: junitparser merge [-h] [--glob] paths [paths ...] output

positional arguments:
  paths       Original XML path(s).
  output      Merged XML Path, setting to "-" will output console

optional arguments:
  -h, --help  show this help message and exit
  --glob      Treat original XML path(s) as glob(s).

Test

The tests are written with python unittest, to run them, use pytest:

pytest test.py

Contribute

PRs are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

junitparser-2.1.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

junitparser-2.1.0-py2.py3-none-any.whl (10.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file junitparser-2.1.0.tar.gz.

File metadata

  • Download URL: junitparser-2.1.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for junitparser-2.1.0.tar.gz
Algorithm Hash digest
SHA256 f00128e2515a541f797fc17fe9c81419913218e72fcd711051de23beae47d82e
MD5 d7cb37ca4f92d49e4c341e92a4954454
BLAKE2b-256 78d1c9914f174346a11d63cc547064f5e47c534cac9fd17b4b2034402decfc91

See more details on using hashes here.

File details

Details for the file junitparser-2.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: junitparser-2.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

File hashes

Hashes for junitparser-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d9e0f7c9d35a1865a726c016b66fe2da1b9e4b3cd8ca443c6a24ff05decab8b2
MD5 6880020548bff7edde9b701441a34ad9
BLAKE2b-256 62f888f589986eebd6eca8dcf489e27c475c8e2c38b40859b6e2b2447f64a8c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page