My code till now:
import groovy.xml.XmlParser
import groovy.xml.XmlNodePrinter
// def xmlfile = readFile "./report.xunit"
String fileContents = new File('report.xml').getText('UTF-8')
println('XML File is - ' fileContents)
def xml = new XmlParser()
def testSuites = xml.parseText(fileContents)
println('xml articles are ' testSuites)
testSuites.findAll { it.testcase.'@file'.text().contains('.py') }.each { articles.remove(it) }
def xmlFile = "report.xunit"
new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
XML file content looks like so:
<?xml version="1.0" encoding="UTF-8"?><testsuite errors="0" failures="0" name="TestResults" prefix="my_workspace" skips="0" tests="6" time="28.0"><testcase classname="extra" file="sample.py" name="sample" time="0.0"/><testcase classname="TestItem1" file="myfolder/TestScript.xml" name="TC001" exectime="14.0"/><testcase classname="TestItem1" file="myfolder/TestScript.py" name="TC001_data" exectime="0.0"/><testcase classname="TestItem1" file="myfolder/TestScript.xml" name="TC002" exectime="14.0"/><testcase classname="TestItem1" file="myfolder/TestScript.py" name="TC002_data" exectime="0.0"/><testcase classname="extra" file="final_out.py" name="final_out" time="0.0"/></testsuite>
And my output should be like so:
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" failures="0" name="TestResults" prefix="my_workspace" skips="0" tests="2" time="28.0">
<testcase classname="TestItem1" file="myfolder/TestScript.xml" name="TC001" exectime="14.0"/>
<testcase classname="TestItem1" file="myfolder/TestScript.xml" name="TC002" exectime="14.0"/>
</testsuite>
I will be running the same in jenkins and at that time i will use the readFile function
CodePudding user response:
Maybe that is something that can help you solve the problem:
import groovy.xml.XmlParser
import groovy.xml.XmlNodePrinter
import groovy.xml.XmlUtil
println "Parse the XML file ....."
def parser = new XmlParser()
def xml = parser.parseText(
new File('report.xml').getText('UTF-8')
)
println "Remove everything except XML reports ....."
xml.findAll {
[email protected]('.xml')
}.each { xml.remove(it) }
println "Print to the output file ....."
def printer = new XmlNodePrinter(new PrintWriter(new FileWriter("report.xunit")))
printer.setPreserveWhitespace(true)
printer.print(xml)
println "Done."
CodePudding user response:
My answer also incorporates the fact tests and time attributes of testsuits node has to be calculated. Also XMLNodePrinter is deprecated. Here is the code I suggest.
import groovy.util.XmlParser;
import groovy.xml.XmlUtil;
File inFile = new File('report.xml')
def testsuite = new XmlParser().parseText(inFile.getText('UTF-8'));
testsuite.testcase.each{ if ([email protected]('.py')) testsuite.remove(it) }
testsuite.@tests = testsuite.testcase.size()
testsuite.@time = testsuite.testcase.@exectime*.toFloat().sum()
def outFile = new File('report.xunit')
outFile.setText(XmlUtil.serialize( testsuite ), 'UTF-8')