我正在尝试加载XML文件,然后从文件中删除一些项目,然后将其写回文件
到目前为止我的代码:
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文件内容看起来像:
<?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>
我的输出应该如此:
<?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>
我将在Jenkins中运行相同的内容,当时我将使用ReadFile函数
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这可以帮助您解决问题:
Maybe that is something that can help you solve the problem:
我的答案还包括必须计算测试套件节点的事实测试和时间属性。 XMLNODEPRINTER也被弃用。这是我建议的代码。
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.