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