Skip to main content

Manipulates JUnit/xUnit Result XML files

Project description

https://travis-ci.org/gastlygem/junitparser.svg?branch=master https://coveralls.io/repos/github/gastlygem/junitparser/badge.svg?branch=master

What does it do?

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

There are already a lot of modules that converts JUnit/xUnit XML from a specific format, but you may run into some proprietory or less-known formats and you want to convert them and feed the result to another tool, or, you may want to manipulate the results in your own way. This is where junitparser come into handy.

Why junitparser?

  • Functionality. There are various JUnit/xUnit XML libraries, some does parsing, some does XML generation, some does manipulation. This module tries to do most functions in a single package.

  • Extensibility. JUnit/xUnit is hardly a standardized format. The base format is somewhat universally agreed with, but beyond that, there could be “custom” elements and attributes. junitparser aims to support them all, by monkeypatching and subclassing some base classes.

  • Pythonic. You can manipulate test cases and suites in a pythonic way.

Installation

pip install junitparser

Usage

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')
case1.result = Skipped()
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.delete_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('/path/to/junit.xml')
for suite in result:
    # handle suites
    for case in suite:
        # handle cases
xml.write() # Writes back to file

Merge XML files

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

from junitparser import JUnitXml

xml1 = JUnitXml('/path/to/junit1.xml')
xml2 = JUnitXml('/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'

Create 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

Test

You can run the cases directly:

python test.py

Or use pytest:

pytest test.py

Notes

There are some other packages providing similar functionalities. They are out there for a longer time, but might not be as fun as junitparser:

  • xunitparser: Read JUnit/XUnit XML files and map them to Python objects

  • xunitgen: Generate xUnit.xml files

  • xunitmerge: Utility for merging multiple XUnit xml reports into a single xml report.

  • junit-xml: Creates JUnit XML test result documents that can be read by tools such as Jenkins

Contribute

Please do!

Changelog

1.0.0

  • Python 2 support. Thanks to @SteinHeselmans

0.9.0

  • Supports xmls with testcase as root node.

  • First beta release.

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-1.0.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distributions

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

junitparser-1.0.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

junitparser-1.0.0-py2-none-any.whl (10.1 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: junitparser-1.0.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for junitparser-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6c9257dc1686de790a6f3cec7a257343839e3fc9bd48f9a8d473cfd9e93b5499
MD5 794911da541c1c3010fe4982c7ffbb6e
BLAKE2b-256 084fe8aa678927a15a1a410731cc0edf36bd540a997c563cd3d60b133cb8273a

See more details on using hashes here.

File details

Details for the file junitparser-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for junitparser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 789b99899f1545675ec09957dcc605a6bb88de322ea9d1e5c1dbd288b682835a
MD5 684312d89aa4f8a3334c4b26a808c888
BLAKE2b-256 0886f0fc34d79c01fd2b8e2d111c4ccf120410101a9c0f199af87f417013a6a2

See more details on using hashes here.

File details

Details for the file junitparser-1.0.0-py2-none-any.whl.

File metadata

File hashes

Hashes for junitparser-1.0.0-py2-none-any.whl
Algorithm Hash digest
SHA256 5b0f0ffeef3548878b5ae2cac40b5b128ae18337e2a260a8265f5519b52c907c
MD5 daaae9b4c3f239a5787bf9ce1e81e37f
BLAKE2b-256 7e599b89bd435f7dd35ce1f488accc516a69b89cf4fead2c016f923fb91a426a

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