标签:
import com.eviware.soapui.SoapUI def getReportToXML(){ def builder = new groovy.xml.StreamingMarkupBuilder() builder.encoding="UTF-8" def Report = { mkp.xmlDeclaration() Report(type:"SoapUI",version:SoapUI.SOAPUI_VERSION ){ passedTotal=0 failedTotal=0 warningsTotal=0 testcaseTotal=0 runner.getResults().each{tsuite -> passed=0 failed=0 warnings=0 tsuite.getResults().status.each{ if(it.toString()=="FINISHED"){ passed=passed+1 } else if(it.toString()=="FAILED"){ failed=failed+1 } else if(it.toString()=="WARNING"){ warnings=warnings+1 } } testcaseTotal=testcaseTotal+tsuite.getResults().size() //log.info tsuite.getResults().size() TestSuite(name:tsuite.testSuite.getName(),total:tsuite.getResults().size(),status:tsuite.getStatus(),runtime:tsuite.getTimeTaken()/1000,stime:tsuite.getStartTime(),passed:passed,failed:failed){ tsuite.getResults().each{tcase -> passed=0 failed=0 warnings=0 tcase.getResults().status.each{ if(it.toString()=="OK"){ passed=passed+1 passedTotal=passedTotal+1 } else if(it.toString()=="FAILED"){ failed=failed+1 failedTotal=failedTotal+1 } else if(it.toString()=="WARNING"){ warnings=warnings+1 warningsTotal=warningsTotal+1 } } TestCase(name:tcase.testCase.getName(),status:tcase.getStatus(),runtime:tcase.getTimeTaken()/1000,stime:tcase.getStartTime(),passed:passed,failed:failed,warnings:warnings){ tcase.getResults().each{tstep -> TestStep(name:tstep.testStep.getName(),status:tstep.getStatus(),runtime:tstep.getTimeTaken()/1000){ tstep.getMessages().each{message -> Message(){ mkp.yieldUnescaped "<![CDATA[${message.toString()}]]>" } } RequestHeaders(){ mkp.yieldUnescaped "<![CDATA[${tstep.getRequestHeaders()}]]>" } } } } } } } Summary(name:project.name,total:testcaseTotal,stime:project.getPropertyValue("stime"),etime:new Date().format(‘yyyy-MM-dd HH:mm:ss‘),status:runner.getStatus(), runtime:runner.getTimeTaken()/1000,passed:passedTotal,failed:failedTotal,warnings:warningsTotal) } } //def writer = new FileWriter("${getReportPath()}${new Date().format(‘yyMMddHHmmss‘)}.xml") builder.bind(Report) //writer << xml } def getReportPath(filename){ matcher = project.path.toString() =~ /.*\// File f = new File("${matcher[0]}report/${project.name}/") f.mkdirs() new File("${matcher[0]}report/${project.name}/${filename}") } def getXML=getReportToXML() //log.info getXML def reportXML = new XmlParser().parseText(getXML.toString()) //log.info reportXML["Summary"][0].attribute("name") def sw =new StringWriter() def html =new groovy.xml.MarkupBuilder(sw) def htmlStyle=‘‘‘ table.gridtable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #666666; border-collapse: collapse; } table.gridtable th { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; } table.gridtable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; } p.request{ color: red; font-size: medium; } ‘‘‘ html.html{ head{ meta("http-equiv":"Content-Type",content:"text/html;charset=utf-8") title("Report-${project.name}") style(type:"text/css",htmlStyle) } body{ div("style":"width: 80%;margin-left: auto;margin-right: auto;",align:"center"){ h1(align:"center","${project.name} 执行报告") h3(align:"left","总览") table(align:"center","class":"gridtable",width:"100%"){ tr(){ th("开始时间") th("结束时间") th("耗时(s)") th("结果") th("总数") th("成功数") th("失败数") //th("警告") } tr(){ td(reportXML["Summary"][0].attribute("stime")) td(reportXML["Summary"][0].attribute("etime")) td(reportXML["Summary"][0].attribute("runtime")) td(reportXML["Summary"][0].attribute("status")) td(reportXML["Summary"][0].attribute("total")) td(reportXML["Summary"][0].attribute("passed")) td(reportXML["Summary"][0].attribute("failed")) //td(reportXML["Summary"][0].attribute("warnings")) } } h3(align:"left","测试集") table(align:"center","class":"gridtable",width:"100%"){ tr(){ th("名称") th("耗时(s)") th("结果") th("总数") th("成功数") th("失败数") } reportXML.TestSuite.each{tsuite -> tr(){ //log.info tsuite td(tsuite.attribute("name")) td(tsuite.attribute("runtime")) td(tsuite.attribute("status")) td(tsuite.attribute("total")) td(tsuite.attribute("passed")) td(tsuite.attribute("failed")) } } } reportXML.TestSuite.each{tsuite -> h4(align:"left",tsuite.attribute("name")) table(align:"center","class":"gridtable",width:"100%"){ tr(){ th("名称") th("耗时(s)") th("结果") } tsuite.TestCase.each{tcase-> tr(){ td(tcase.attribute("name")) td(tcase.attribute("runtime")) td(tcase.attribute("status")) } if(tcase.attribute("status").toString()=="FAILED"){ tr(){ td(colspan:"3"){ tcase.TestStep.each{tstep-> if(tstep.attribute("status")=="FAILED"){ p("class":"request","名称:${tstep.attribute("name")} 耗时:${tstep.attribute("runtime")}") tstep.Message.each{message-> p(message.text()) } } } } } } } } p(align:"right"){a(href:"javascript:scroll(0,0)","返回顶部")} } } } } def reportTime=new Date().format(‘yyMMddHHmmss‘) getReportPath("${reportTime}.html").write(sw.toString()) def writer=new FileWriter(getReportPath("${reportTime}.xml")) //log.info sw.toString() writer.write(getXML)
标签:
原文地址:http://www.cnblogs.com/zhangminjie/p/4216038.html