groovy template help.

257 views
Skip to first unread message

Bubunia Patra

unread,
Nov 24, 2016, 12:21:21 AM11/24/16
to Jenkins Users
Hi all,

I am new to groovy and the syntax is very confusing and error prone and written a template to work with Email-Extn plugin. I want to add the test result in the tabular format as below. Can anyone help me in this regard?

Regards
Pradeep

For exp if 1 test executed then i want a table in the email-body as follows: 

TestName | Test Status | Pass | Fail | Total
     
abc          |  Pass          |  1     |  0 |  1

<table cellspacing="0" cellpadding="2" border="1">
<thead>
 <tr bgcolor="#F3F3F3">
  <td class="header">Test Name</td>
  <td class="header">Test Status</td>
  <td class="header">Pass</td>
  <td class="header">Fail</td>
  <td class="header">Total</td>
 </tr>
</thead>
</table>

<% def junitResultList = it.JUnitTestResult
try {
 def cucumberTestResultAction = it.getAction("org.jenkinsci.plugins.cucumber.jsontestsupport.CucumberTestResultAction")
 junitResultList.add(cucumberTestResultAction.getResult())
} catch(e) {
        //cucumberTestResultAction not exist in this build
}
if (junitResultList.size() > 0) { %>
 <TABLE width="100%">
 <TR><TD class="bg1" colspan="2"><B>${junitResultList.first().displayName}</B></TD></TR>
 <% junitResultList.each{
  junitResult -> %>
     <% junitResult.getChildren().each { packageResult -> %>
        <TR><TD class="bg2" colspan="2"> ${packageResult.getName()} ${packageResult.getFailCount()},${packageResult.getPassCount()},${packageResult.getSkipCount()},${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()}</TD></TR>
        <% packageResult.getFailedTests().each{ failed_test -> %>
          <TR bgcolor="white"><TD class="test_failed" colspan="2"><B><li>Failed: ${failed_test.getFullName()} </li></B></TD></TR>
        <% }
      }
 } %>
 </TABLE>
 <BR/>
<%
} %>

Baptiste Mathus

unread,
Nov 24, 2016, 1:15:16 AM11/24/16
to jenkins...@googlegroups.com
So what does not work? What is the error?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/cc06fcf9-7be4-40ad-8437-a5da816206b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bubunia Patra

unread,
Nov 24, 2016, 5:55:44 AM11/24/16
to Jenkins Users, m...@batmat.net


On Thursday, November 24, 2016 at 11:45:16 AM UTC+5:30, Baptiste Mathus wrote:
So what does not work? What is the error?
Code : I want to print TestName after parsing the JUNIT xml. I want to parse the jUNIT and fill the table,

<table cellspacing="0" cellpadding="2" border="1">
<thead>
 <tr bgcolor="#F3F3F3">
  <td class="header">Test Name
  <% def junitResultList = it.JUnitTestResult
  try {
 def cucumberTestResultAction = it.getAction("org.jenkinsci.plugins.cucumber.jsontestsupport.CucumberTestResultAction")
 junitResultList.add(cucumberTestResultAction.getResult())
} catch(e) {
        //cucumberTestResultAction not exist in this build
  }
 if (junitResultList.size() > 0) { %>
 <TABLE width="100%">
 <TR><TD class="bg1" colspan="2"><B>${junitResultList.first().displayName}</B></TD></TR> 
  <% junitResultList.each{
  junitResult -> %>
     <% junitResult.getChildren().each { packageResult -> %>
        <TR><TD class="bg2" colspan="2"> ${packageResult.getName()} }  </TD></TR>
<% } %>
  </td>
  <td class="header">Test Status</td>
  <td class="header">Pass</td>
  <td class="header">Fail</td>
  <td class="header">Total</td>
 </tr>
</thead>
</table> 

It fails with Parsing Error. The error message is also not clear. 

Exception raised during template rendering: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: SimpleTemplateScript61.groovy: 424: expecting '}', found '' @ line 424, column 40. /* Generated by SimpleTemplateEngine */ ^ 1 error groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed: SimpleTemplateScript61.groovy: 424: expecting '}', found '' @ line 424, column 40. /* Generated by SimpleTemplateEngine */ ^ 1 error at groovy.text.SimpleTemplateEngine.createTemplate(SimpleTemplateEngine.java:125) at groovy.text.TemplateEngine.createTemplate(TemplateEngine.java:38) at hudson.plugins.emailext.plugins.content.ScriptContent.renderTemplate(ScriptContent.java:128) at hudson.plugins.emailext.plugins.content.ScriptContent.evaluate(ScriptContent.java:69) at hudson.plugins.emailext.EmailExtTemplateAction.renderTemplate(EmailExtTemplateAction.java:132) at sun.reflect.GeneratedMethodAccessor1346.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.kohsuke.stapler.Function$InstanceFunction.invoke(Function.java:298) at org.kohsuke.stapler.Function.bindAndInvoke(Function.java:161) at org.kohsuke.stapler.Function.bindAndInvokeAndServeResponse(Function.java:96) at 


Is there a document I can refer for syntax to while writing groovy template script?

Regards
Pradeep
 
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

jer...@bodycad.com

unread,
Nov 24, 2016, 1:17:32 PM11/24/16
to Jenkins Users, m...@batmat.net
Hi,
I can give you my template I used to have when non pipeline (haven't tested it into the pipeline, I went Jelly). Here's the code for JUnit part
<!-- JUnit TEMPLATE -->
<% def junitResultList = it.JUnitTestResult
if (junitResultList.size() > 0) { %>
  <TABLE width="100%">
    <TR style="${style_tr}">
<TD style="${style_bg1}" colspan="2"><B>JUnit Tests</B></TD>
</TR>
    <% it.JUnitTestResult.each{ junitResult ->
      junitResult.getChildren().each { packageResult -> %>
        <TR style="${style_tr}">
<TD style="${style_bg2}" colspan="2"> Name: ${packageResult.getName()} Failed: ${packageResult.getFailCount()} test(s), Passed: ${packageResult.getPassCount()} test(s), Skipped: ${packageResult.getSkipCount()} test(s), Total: ${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()} test(s)</TD>
</TR>
        <% packageResult.getFailedTests().each{ failed_test -> %>
<TR style="${style_tr}">
<TD style="${style_bg3}${style_failure}" colspan="2"><B><li>Failed: ${failed_test.getFullName()} </li></B></TD>
</TR>
        <% }
      }
    }
} %>
  </TABLE>

For the Jelly I end up with the following:
<!-- XUnit TEMPLATE -->
<TABLE width="100%">
<TR>
<TD colspan="2"><B>Unit Tests</B></TD>
</TR>
<j:forEach var="junitResult" items="${it.JUnitTestResult}">
<j:forEach var="packageResult" items="${junitResult.getChildren()}">
<TR>
<TD>Tests Root Name</TD>
<TD>${packageResult.getName()}</TD>
</TR>
<TR>
<TD>Tests Counts</TD>
<TD>${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()}</TD>
</TR>
<TR>
<TD>Tests Pass</TD>
<TD style=" ${style_success}">${packageResult.getPassCount()}</TD>
</TR>
<TR>
<TD>Tests Skip</TD>
<TD style="${style_unstable}">${packageResult.getSkipCount()}</TD>
</TR>
<TR>
<TD>Tests Fail</TD>
<TD>${packageResult.getFailCount()}</TD>
</TR>
<j:forEach var="failed_test" items="${packageResult.getFailedTests()}">
<TR>
<TD colspan="2"><B><li>Failed: ${failed_test.getFullName()} </li></B></TD>
</TR>
</j:forEach>
</j:forEach> 
</j:forEach>
</TABLE>
<BR/>


jer...@bodycad.com

unread,
Nov 24, 2016, 1:19:38 PM11/24/16
to Jenkins Users, m...@batmat.net
For the template part, here's some header variables (I do the same with jelly but I replace the value before giving the template to emailExt):

<!-- Local variables -->
<%
style_default = "font-family:Verdana,Helvetica,sans serif; font-size:11px; color:black;"
style_header = "color: black;"
style_bg1 = "color:white; background-color:" + style_cie_color + "; font-size:120%;"
style_bg2 = "color:#666666; background-color:white; font-size:110%; margin-left: 2px; margin-right: 2px;"
style_bg3 = "color: black; background-color:white;"
style_tr = "border: 1px solid " + style_cie_color + ";"
style_failure = "color: red;"
style_success = "color: green;"
style_unstable = "color: yellow;"
...
%>


Bubunia Patra

unread,
Nov 24, 2016, 9:53:40 PM11/24/16
to Jenkins Users, m...@batmat.net
Hi Jerome,

I want all the information in tabular format. If you refer to my original email i had the JUNIT groovy template that you have.

Your/My original template will actually print something like this:

Testcase | Status | Pass | Fail | Skipped | Total
TestResult:
Name : abc
Status : Pass
Pass = 1
Fail = 0
Skipped=0
Total=1

Expected Output:

TestCase | Status | Pass | Fail | Skipped | Total 
abc            pass     1         0       0         | 1


Regards
Pradeep
Reply all
Reply to author
Forward
0 new messages