Home > OS >  Python Module To Convert JUnit XML Report to Pretty Console Output
Python Module To Convert JUnit XML Report to Pretty Console Output

Time:11-17

I am running android gradle tests which outputs a generic JUnit XML Report as shown below. I am trying to ideally find a Python extension or easy method to also print these results to console.

I am capable of converting the XML to html with Python plugins which is useful in certain scenarios. But this is not ideal as it requires extra steps to open and requires a GUI which is not always possible. I can print the XML directly but it's not very clean. We want a direct clean printout.

For example this is my XML file

 <?xml version="1.0"?>
<testsuites>
  <testsuite name="My test suite 1" tests="2" failures="0" skipped="0" timedout="0" errors="0" time="316.032" timestamp="2022-11-10 21:43:40  0000">
    <properties>
      <property name="id" value="28394"/>
      <property name="device" value="Samsung Galaxy S10"/>
    </properties>
    <testcase name="test library" classname="library" result="passed" test_id="1" time="7.775"/>
    <testcase name="test package" classname="package" result="passed" test_id="2" time="7.986"/>
  </testsuite>
  <testsuite name="test suite 2" tests="1" failures="1" skipped="0" timedout="0" errors="0" time="193.795" timestamp="2022-11-10 21:55:10  0000">
    <properties>
      <property name="id" value="239548"/>
      <property name="device" value="Samsung Galaxy S10"/>
    </properties>
    <testcase name="test API" classname="apiTest" result="failed" test_id="1" time="193.795" >
      <failure>
      Failure message will be properly filled in
      </failure>
    </testcase>
  </testsuite>
</testsuites>
... 

And I would like it to print something as follows

 My test suite 1
     timedout=0
     timestamp=2022-11-10 21:43:40  0000
     id=28394
     device=Samsung Galaxy S10

     ✓ test library time=7.775
     ✓ test package time=7.986

 test suite 2
     timedout=0
     timestamp=2022-11-10 21:55:10  0000
     id=239548
     device=Samsung Galaxy S10

     ✗ test API time=193.795
       - Failure message will be properly filled in

2 passed, 1 failure

What I tried so far is;

  • xunit-viewerworks perfectly with option --console, exactly what I wanted. However I rather have a python package as its easier to assure multiple systems have the same setup by sharing a pipenv and allowing everything to run within python. Instead of having to install npm followed by this package and running externally.
  • Tried using junit2html to create a html file with option to output summary matrix, but this does not include detailed info, like failure logs, only a brief summary.
  • Attempted with junit_xml to do xml=JUnitXml.fromfile(<FILE>) followed by xml.tostring() but this prints it in XML format, and not a pretty output.
  • Tried using import xml.etree.ElementTree as ET and iterating through the elements to custom print them. But I rather have a defined module that does this as I may not cover all possible scenarios, etc. that may not be present in my file.

CodePudding user response:

Write custom code to handle this situation

def main(xml_path):
    tree = ET.parse(xml_path)
    root = tree.getroot()

    for child in root.iter():

        if child.tag == "testsuite":
           # Add code for all cases, via `child.attrib or child.text`

  • Related