Package: junitparsershubhindia
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.FeaturesParse 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, orpython -m junitparserPython 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 2Version 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 thatTestCase.resultis now a list instead of a single item.
If you are using this attribute, please update your code accordingly.Installationpip install junitparserUsageYou should be relatively familiar with the Junit XML format. If not, runpydocon the exposed classes and functions to see how it’s structured.Create Junit XML format reports from scratchYou have some test result data, and you want to convert them into junit.xml
format.fromjunitparserimportTestCase,TestSuite,JUnitXml,Skipped,Error# Create casescase1=TestCase('case1','class.name',0.5)# params are optionalcase1.classname="modified.class.name"# specify or change case attrscase1.result=[Skipped()]# You can have a list of resultscase2=TestCase('case2')case2.result=[Error('Example error message','the_error_type')]# Create suite and add casessuite=TestSuite('suite1')suite.add_property('build','55')suite.add_testcase(case1)suite.add_testcase(case2)suite.remove_testcase(case2)# Add suite to JunitXmlxml=JUnitXml()xml.add_testsuite(suite)xml.write('junit.xml')Read and manipulate exiting JUnit/xUnit XML filesYou have some existing junit.xml files, and you want to modify the content.fromjunitparserimportJUnitXmlxml=JUnitXml.fromfile('/path/to/junit.xml')forsuiteinxml:# handle suitesforcaseinsuite:# handle casesxml.write()# Writes back to fileIt is also possible to use a custom parser. For example lxml provides a plethora
of parsingoptions. We can use them this way:fromlxml.etreeimportXMLParser,parsefromjunitparserimportJUnitXmldefparse_func(file_path):xml_parser=XMLParser(huge_tree=True)returnparse(file_path,xml_parser)xml=JUnitXml.fromfile('/path/to/junit.xml',parse_func)# process xml...Merge XML filesYou have two or more XML files, and you want to merge them into one.fromjunitparserimportJUnitXmlxml1=JUnitXml.fromfile('/path/to/junit1.xml')xml2=JUnitXml.fromfile('/path/to/junit2.xml')newxml=xml1+xml2# Alternatively, merge in placexml1+=xml2Note that it won’t check for duplicate entries. You need to deal with them on
your own.Create XML with custom attributesYou want to use an attribute that is not supported by default.fromjunitparserimportTestCase,Attr,IntAttr,FloatAttr# Add the custom attributeTestCase.id=IntAttr('id')TestCase.rate=FloatAttr('rate')TestCase.custom=Attr('custom')case=TestCase()case.id=123case.rate=0.95case.custom='foobar'Handling XML with custom elementThere may be once in 1000 years you want to it this way, but anyways.
Suppose you want to add element CustomElement to TestCase.fromjunitparserimportElement,Attr,TestSuite# Create the new element by subclassing Element,# and add custom attributes to it.classCustomElement(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:forcustomintestcase.iterchildren(CustomElement):...# Do things with custom elementHandling custom XML attributesSay you have some data stored in the XML as custom attributes and you want to
read them out:fromjunitparserimportTestCase,Attr,JUnitXml# Create the new element by subclassing Element or one of its child class,# and add custom attributes to it.classMyTestCase(TestCase):foo=Attr()xml=JUnitXml.fromfile('/path/to/junit.xml')forsuiteinxml:# handle suitesforcaseinsuite:my_case=MyTestCase.fromelem(case)print(my_case.foo)Command Line$junitparser--helpusage:junitparser[-h][-v]{merge}...JunitparserCLIhelper.positionalarguments:{merge}commandmergeMergeJunitXMLformatreportswithjunitparser.optionalarguments:-h,--helpshowthishelpmessageandexit-v,--versionshowprogram'sversionnumberandexit$junitparsermerge--helpusage:junitparsermerge[-h][--glob]paths[paths...]outputpositionalarguments:pathsOriginalXMLpath(s).outputMergedXMLPath,settingto"-"willoutputconsoleoptionalarguments:-h,--helpshowthishelpmessageandexit--globTreatoriginalXMLpath(s)asglob(s).TestThe tests are written with pythonunittest, to run them, use pytest:pytest test.pyContributePRs are welcome!