Skip to main content

Manipulates JUnit/xUnit Result XML files

Project description

https://travis-ci.org/gastlygem/junitparser.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.

  • Simplicity. One single code file. No external dependencies. Though it will use lxml if available.

Installation

pip install junitparser

Usage

Create Junit XML format reports from scratch

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

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

from junitparser import JUnitXml

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

newxml = xml1 + xml2
# Alternatively, merge inplace
xml1 += xml2

Create XML with custom attributes

from junitparser import TestCase, Attr

# The id attribute is not supported by default
# But we can support it by monky patching
TestCase.id = Attr('id')
case = TestCase()
case.id = '123'

print(case.tostring())

And you get the following output:

b'<testcase id="123"/>\n'

Create XML with custom element

There may be once in 1000 years you want to it this way, but anyways:

from junitparser import Element, Attr, TestSuite

# Suppose you want to add element Custom to TestSuite.
# You can create the new element by subclassing Element,
# Then add custom attributes to it.
class Custom(Element):
    _tag = 'custom'
    foo = Attr()
    bar = Attr()

# Then monkeypatch TestSuite to handle Custom
# TODO: Tricky part, update later

TODO

  • XML 1.0 and 1.1 compatibilities.

  • More tests, especially test for errors.

Notes

Python 2 is not supported. Currently there is no plan to support Python 2.

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

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-0.1.zip (10.9 kB view details)

Uploaded Source

Built Distribution

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

junitparser-0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file junitparser-0.1.zip.

File metadata

  • Download URL: junitparser-0.1.zip
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for junitparser-0.1.zip
Algorithm Hash digest
SHA256 23a9765da8c8b02bef02f37d3125ca874d6c2f64bfc4200f77596408fd3faf5b
MD5 0c56405f60eb7e25bd7ac8d54b99ca5b
BLAKE2b-256 3c37d55291108e87f8eb19515227febce85d400b1d762ecc2eedf02fa0633b13

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for junitparser-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 468d5cba7e2fa514dc4e3655004e5f8239bf9b21077b18855e8cb210354f8e8f
MD5 8d43859ae2da3aa05d4033543e6f9fd4
BLAKE2b-256 3f56d5d824f38dc2b4636d681b8d960605814cee2dce8faa49ed1079ae9b37be

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