0

I am creating a customized report from soapui TESTS-TestSuites.xml file.

Below is the result in xml format -

<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
  <testsuite errors="0" failures="0" id="0" name="APIs and API Versions retrieval" package="PAD API Regression" tests="11" time="1.535">
  <testcase name="GET-APIs" time="0.942" />
  <testcase name="GET-flight-inspiration-search-SearchnShoppingFamily" time="0.064" />
  <testcase name="GET-top-flight-destinations_SearchnShoppingFamily" time="0.059" />
  <testcase name="GET-flight-search-by-calendar_SearchnShoppingFamily" time="0.058" />
  <testcase name="GET-top-flight-destinations_TravelIntelligenceFamily" time="0.064" />
  <testcase name="GET-fare-search-history_TravelIntelligenceFamily" time="0.089" />
  <testcase name="GET-checked-in-links_UtilitiesFamily" time="0.067" />
  <testcase name="GET-nearest-relevant-airport_UtilitiesFamily" time="0.062" />
  <testcase name="GET-airport-lists-by-city/country_UtilitiesFamily" time="0.054" />
  <testcase name="GET-hotel-search_HotelAPIsFamily" time="0.052" />
  <testcase name="GET-APIs_with_invalid_ID" time="0.024" />
 </testsuite>
<testsuite errors="0" failures="1" id="1" name="Service and API Catalogue" package="PAD API Regression" tests="13" time="0.629">
  <testcase name="GET-List_of_catalogues" time="0.058" />
  <testcase name="GET-Catalogue_AirlineIT2" time="0.01">
     <failure message="Cancelling due to failed test step" type="Cancelling due to failed test step">
  <![CDATA[<h3><b>Catalogue_AirlineIT2 Failed</b></h3><pre>[Valid HTTP Status Codes] Response status code:200 is not in acceptable list of status codes
</pre><hr/>]]>
     </failure>
  </testcase>
   <testcase name="GET-Catalogue_Distribution" time="0.05" />
   <testcase name="GET-Catalogue_Self-service" time="0.035" />
   <testcase name="GET-All_Families_of_Self-Service_Catalogue" time="0.065" />
   <testcase name="GET-Families_Level_1_of_Self-Service_Catalogue" time="0.053" />
   <testcase name="GET-Families_Level_2_of_Self-Service_Catalogue" time="0.058" />
   <testcase name="GET-Family_Air API_of_Self-Service_Catalogue" time="0.03" />
   <testcase name="GET-Family_Hotel API_of_Self-Service_Catalogue" time="0.067" />
   <testcase name="GET-Family_Search_and_Shopping_from_Air_APIs_of_Self-Service_Catalogue" time="0.052" />
   <testcase name="GET-Family_Travel Intelligence API_of_Self-Service_Catalogue" time="0.036" />
   <testcase name="GET-Family_Utilities API_of_Self-Service_Catalogue" time="0.105" />
   <testcase name="GETFamily_Search_and_Shopping_from_Hotel_API_of_Self-Service_Catalogue" time="0.01" />
  </testsuite>
</testsuites>

Below is my code to read and get result for the report -

import os
import os.path
import codecs
import xml.etree.ElementTree
import xml.etree.ElementTree as ET
from BeautifulSoup import BeautifulSoup
from shutil import copyfile

data = open(r'H:\Scripts\report\TESTS-TestSuites.xml')
root = ET.parse(data).getroot()
projectName = root.find('testsuite').get('package')
totalTime = 0
totalTestCases = 0
totalTestFailure = 0
for testsuites in root.findall('testsuite'):    
    totalTestCases += int(testsuites.get('tests'))
    totalTime += float(testsuites.get('time'))
    totalTestFailure += int(testsuites.get('failures'))
successRate = "%.2f" % ((float(totalTestCases) - float(totalTestFailure))/float(totalTestCases)*100)
print "SuccessRate", successRate

# Get All Test Cases
for testcases in root.iter('testcase'):
    print testcases.get('name')

# Get All Failed Test Cases 
for testcases in root.getiterator():
     if testcases.tag == 'failure':
        print testcases.text

# Get All Failed TestSuites 
for testsuites in root.findall('testsuite'):
   if int(testsuites.get('failures')) !=0:
      print "Failed TestSuite name", testsuites.get('name')

How can I get the name of TestCase which is failed.

If I do following, I get None

for testsuites in root.findall('testsuite'):
   if int(testsuites.get('failures')) !=0:
      print "Failed TestSuite name", testsuites.get('name')
      for testcases in root.getiterator():
         if testcases.tag == 'failure':
            print testcases.get('name')
Rao
  • 20,781
  • 11
  • 57
  • 77
royki
  • 1,593
  • 3
  • 25
  • 45
  • If your object is to create html report, then use `apache-ant` and provide the above xml as input. See here for more details. - https://stackoverflow.com/questions/12855740/coversion-of-junit-xml-report-into-html-form – Rao Jun 26 '17 at 09:41
  • I checked that. My intention is to create report like damage-control-report . . Take a look here please - my other question - https://stackoverflow.com/questions/44453012/surefire-report-generating-duplicate-result-in-the-report Check the below portion of my question please. And if you have any idea , would be great. – royki Jun 26 '17 at 09:59
  • Do not have much idea into python or maven. Do you mind using `groovy` to process the xml? – Rao Jun 26 '17 at 12:21
  • Definitely not. Eventually I already got help from your groovy script in my soapui project. ;-) https://stackoverflow.com/questions/41700437/creating-a-test-report-from-project-level-tear-down-script/41759553#41759553 – royki Jun 26 '17 at 13:46
  • Oh glad to know that. May be you up vote the helpful answer ;) – Rao Jun 26 '17 at 13:56
  • But I would likely to have in damage-control report. – royki Jun 26 '17 at 15:25
  • @Rao Is it possible to have in groovy ? – royki Jun 27 '17 at 09:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147699/discussion-between-rao-and-altius). – Rao Jun 27 '17 at 09:52
  • @Rao hello Rao, Can you please check this, if you have time https://stackoverflow.com/questions/46298419/transfer-property-ids-array-to-other-testcases-in-soapui-groovy/46307286#46307286 Thanks and Regards – royki Sep 19 '17 at 22:14

1 Answers1

1

Based on the discussion on the chat, OP requested for a groovy script which reads the xml file as mentioned in the question and write a separate xml file for each testcase.

Below uses template approach to write the file.

Using a default map file to be replaced in the template. For each instance, replace the values from the respective testcase into template.

Here is the Groovy Script:

//Provide the absolute path of the report xml
def inputXml = '/absolute/path/report.xml'

//Define the location where files needs to be written
def outputDir = '/absolute/path/output/directory'

def templateXml = '''<?xml version="1.0" encoding="UTF-8"?> 
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="$className" time="$suiteTime" tests="$tests" errors="$errors" skipped="$skipped" failures="$failures"> 
<testcase name="$caseName" classname="$className" time="$caseTime"/> 
</testsuite>'''

def binding = [failures:'0', errors: '0', skipped:'0', suiteTime: '0.00', caseTime:'0.00', caseName:'', tests: '1', suite: '', className: 'com.amadeus.developers.pad.amp.catalogue.Catalogue_TryIt_002_SoapSpec']

def xml = new XmlSlurper().parseText(new File('report.xml').text)
def testcases = xml.'**'.findAll{it.name() == 'testcase'}
def engine = new groovy.text.SimpleTemplateEngine()

//Save the contents to a file
def saveToFile(file, content) {
    if (!file.parentFile.exists()) {
         file.parentFile.mkdirs()
         println "Directory did not exist, created"
    }
    file.write(content) 
    assert file.exists(), "${file.name} not created"
}

def writeCaseData = { kase ->
    def bindingKase = binding.clone()
    bindingKase.errors = kase.parent().@errors
    bindingKase.suiteTime = kase.parent().@time
    bindingKase.suite = kase.parent().@name
    bindingKase.caseName = kase.@name
    bindingKase.caseTime = kase.@time
    kase.failure.size() == 0 ?: (bindingKase.failures = '1')
    def template = engine.createTemplate(templateXml).make(bindingKase)
    saveToFile(new File("${outputDir}/${kase.@name}.xml"), template.toString())
}

testcases.each { writeCaseData it }
Rao
  • 20,781
  • 11
  • 57
  • 77