junitparser.junitparser module

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.

Reference schema: https://github.com/windyroad/JUnit-Schema/blob/master/JUnit.xsd

This, according to the document, is Apache Ant’s JUnit output.

See the documentation for other supported schemas.

class junitparser.junitparser.Attr(name: str = None)[source]

Bases: object

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

class junitparser.junitparser.Element(name: str = None)[source]

Bases: object

Base class for all JUnit XML elements.

append(sub_elem)[source]

Add the element subelement to the end of this elements internal list of subelements.

child(Child)[source]

Find a single child of specified Child type.

extend(sub_elems)[source]

Add elements subelement to the end of this elements internal list of subelements.

classmethod fromelem(elem)[source]

Construct JUnit objects from an ElementTree element elem.

classmethod fromstring(text: str)[source]

Construct JUnit object cls from XML string test.

iterchildren(Child)[source]

Iterate through specified Child type elements.

remove(sub_elem)[source]

Remove subelement sub_elem.

tostring()[source]

Convert element to XML string.

class junitparser.junitparser.Error(message: str = None, type_: str = None)[source]

Bases: Result

Test result when the case has errors during execution.

class junitparser.junitparser.Failure(message: str = None, type_: str = None)[source]

Bases: Result

Test result when the case failed.

class junitparser.junitparser.FloatAttr(name: str = None)[source]

Bases: Attr

A float attribute for an XML element.

This class is used internally for counting test durations, but you could use it for any specific purpose.

class junitparser.junitparser.IntAttr(name: str = None)[source]

Bases: Attr

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

class junitparser.junitparser.JUnitXml(name=None)[source]

Bases: Element

The JUnitXml root object.

It may contain <TestSuites> or a <TestSuite>.

name

Name of the testsuite if it only contains one testsuite.

time

Time consumed by the testsuites.

tests

Total number of tests.

failures

Number of failed cases.

errors

Number of cases with errors.

skipped

Number of skipped cases.

add_testsuite(suite: TestSuite)[source]

Add a testsuite.

errors

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

failures

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

classmethod fromfile(filepath: str, parse_func=None)[source]

Initiate the object from a report file.

classmethod fromroot(root_elem: Element)[source]

Construct JUnit objects from an elementTree root element.

classmethod fromstring(text: str)[source]

Construct JUnit objects from an XML string.

name

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

skipped

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

tests

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

time

A float attribute for an XML element.

This class is used internally for counting test durations, but you could use it for any specific purpose.

update_statistics()[source]

Update test count, time, etc.

write(filepath: str = None, pretty=False, to_console=False)[source]

Write the object into a JUnit XML file.

If file_path is not specified, it will write to the original file. If pretty is True, the result file will be more human friendly.

exception junitparser.junitparser.JUnitXmlError[source]

Bases: Exception

Exception for JUnit XML related errors.

class junitparser.junitparser.Properties[source]

Bases: Element

A list of properties inside a testsuite.

See Property

add_property(property_: Property)[source]
class junitparser.junitparser.Property(name: str = None, value: str = None)[source]

Bases: Element

A key/value pare that’s stored in the testsuite.

Use it to store anything you find interesting or useful.

name

The property name.

value

The property value.

name

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

value

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

class junitparser.junitparser.Result(message: str = None, type_: str = None)[source]

Bases: Element

Base class for test result.

message

Result as message string.

type

Message type.

message

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

property text
type

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

class junitparser.junitparser.Skipped(message: str = None, type_: str = None)[source]

Bases: Result

Test result when the case is skipped.

class junitparser.junitparser.System(content: str = None)[source]

Bases: Element

Parent class for SystemOut and SystemErr.

text

The output message.

property text
class junitparser.junitparser.SystemErr(content: str = None)[source]

Bases: System

class junitparser.junitparser.SystemOut(content: str = None)[source]

Bases: System

class junitparser.junitparser.TestCase(name: str = None, classname: str = None, time: float = None)[source]

Bases: Element

Object to store a testcase and its result.

name

Name of the testcase.

classname

The parent class of the testcase.

time

The time consumed by the testcase.

classname

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

property is_passed

Whether this testcase was a success (i.e. if it isn’t skipped, failed, or errored).

property is_skipped

Whether this testcase was skipped.

name

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

property result

A list of Failure, Skipped, or Error objects.

property system_err

stderr.

property system_out

stdout.

time

A float attribute for an XML element.

This class is used internally for counting test durations, but you could use it for any specific purpose.

class junitparser.junitparser.TestSuite(name=None)[source]

Bases: Element

The <testsuite> object.

name

The name of the testsuite.

hostname

Name of the test machine.

time

Time consumed by the testsuite.

timestamp

When the test was run.

tests

Total number of tests.

failures

Number of failed tests.

errors

Number of cases with errors.

skipped

Number of skipped cases.

add_property(name: str, value: str)[source]

Add a property name = value to the testsuite.

See Property and Properties.

add_testcase(testcase: TestCase)[source]

Add a testcase testcase to the testsuite.

add_testcases(testcases: List[TestCase])[source]

Add testcases testcases to the testsuite.

add_testsuite(suite)[source]

Add a testsuite suite to the testsuite.

errors

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

failures

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

hostname

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

name

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

properties()[source]

Iterate through all Property elements in the testsuite.

remove_property(property_: Property)[source]

Remove property property_ from the testsuite.

remove_testcase(testcase: TestCase)[source]

Remove testcase testcase from the testsuite.

skipped

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

tests

An integer attribute for an XML element.

This class is used internally for counting testcases, but you could use it for any specific purpose.

testsuites()[source]

Iterate through all testsuites.

time

A float attribute for an XML element.

This class is used internally for counting test durations, but you could use it for any specific purpose.

timestamp

An attribute for an XML element.

By default they are all string values. To support different value types, inherit this class and define your own methods.

Also see: IntAttr, FloatAttr.

update_statistics()[source]

Update test count and test time.

write(filepath: str = None, pretty=False)[source]
junitparser.junitparser.attributed(cls)[source]

Decorator to read XML element attribute name from class attribute.

class junitparser.junitparser.junitxml(name, bases, methods)[source]

Bases: type

Metaclass to decorate the XML class.

junitparser.junitparser.write_xml(obj, filepath=None, pretty=False, to_console=False)[source]