Hudson/Jenkins默认提供了发送邮件的功能,但是比较有局限性。插件Email-ext提供了比较好的email功能。安装好该插件后,可以在$JENKINS_HOME($HUDSON_HOME)/email-templates目录下定义自己的email模板。下面提供一个jelly script基于junit的测试结果生成email的模板,先上email效果图:


该email report风格比较清新,该有的信息都有了,通过的case用绿色标出,失败的case用红色标出,其他的log, 具体的report, 最近build的整体情况的link也都给出。

对应的jelly script(myreport.jelly)如下:

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
 <body style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
  <j:set var="spc" value="&nbsp;&nbsp;" />
  <!-- GENERAL INFO -->
  <table style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
   <tr>
    <td align="right" style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     <j:choose>
      <j:when test="${build.result=='SUCCESS'}">
       <img src="${rooturl}static/e59dfe28/images/32x32/blue.gif">
       </img>
      </j:when>
      <j:when test="${build.result=='FAILURE'}">
       <img src="${rooturl}static/e59dfe28/images/32x32/red.gif">
       </img>
      </j:when>
      <j:otherwise>
       <img src="${rooturl}static/e59dfe28/images/32x32/yellow.gif">
       </img>
      </j:otherwise>
     </j:choose>
    </td>
    <td valign="center" style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     <b style="font-size: 200%;">
      TESTRUN ${build.result}
     </b>
    </td>
   </tr>
   <tr>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     Project:
    </td>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     ${project.name}
    </td>
   </tr>
   <tr>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     Date of run:
    </td>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     ${it.timestampString}
    </td>
   </tr>
   <tr>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     Test duration:
    </td>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     ${build.durationString}
    </td>
   </tr>
   <tr>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     Test report:
    </td>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     <a href="http://xxxxx:8080/view/job/${project.name}/${build.number}/xxx_build_report/">
      xxxxx Test Report
     </a>
    </td>
   </tr>
    <tr>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     Console logs:
    </td>
    <td style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
     <a href="${rooturl}${build.url}console">
      ${rooturl}${build.url}console
     </a>
    </td>
   </tr>
  </table>
  <br />
  
  <div style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
      <j:set var="healthIconSize" value="16x16" />
      <j:set var="healthReports" value="${project.buildHealthReports}" />
      <j:if test="${healthReports!=null}">
	  <b style="font-size: 160%;">Health Report</b>
	  <br />
        <table>
          <tr>
            <th>W</th>
            <th>Description</th>
            <th>Score</th>
          </tr>
          <j:forEach var="healthReport" items="${healthReports}">
            <tr>
              <td>
                <img src="${rooturl}${healthReport.getIconUrl(healthIconSize)}" />
              </td>
              <td>${healthReport.description}</td>
              <td>${healthReport.score}</td>
            </tr>
          </j:forEach>
        </table>
        <br />
      </j:if>
    </div>
	

  <j:set var="junitResultList" value="${it.JUnitTestResult}" />
    <j:if test="${junitResultList.isEmpty()!=true}">
	  <div style="font-family: Verdana, Helvetica, sans serif; font-size: 11px; color: black">
        <b style="font-size: 160%;">Summary Report</b>
		<br />
        <table style="border: 1px solid black; border-collapse:collapse;">
          <tr>
            <th style="border: 1px solid black; border-collapse:collapse;">Package</th>
            <th style="border: 1px solid black; border-collapse:collapse;">Failed</th>
            <th style="border: 1px solid black; border-collapse:collapse;">Passed</th>
            <th style="border: 1px solid black; border-collapse:collapse;">Skipped</th>
            <th style="border: 1px solid black; border-collapse:collapse;">Total</th>
          </tr>
          <j:forEach var="junitResult" items="${it.JUnitTestResult}">
            <j:forEach var="packageResult" items="${junitResult.getChildren()}">
              <tr>
                <td style="border: 1px solid black; border-collapse:collapse;">
                  <tt>${packageResult.getName()}</tt>
                </td>
                <td style="border: 1px solid black;border-collapse:collapse; color:red;">${packageResult.getFailCount()}</td>
                <td style="border: 1px solid black;border-collapse:collapse; color:blue;">${packageResult.getPassCount()}</td>
                <td style="border: 1px solid black;border-collapse:collapse; color:grey;">${packageResult.getSkipCount()}</td>
                <td style="border: 1px solid black;border-collapse:collapse;">
                  <b>${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()}</b>
                </td>
              </tr>
			  <j:forEach var="passed_test" items="${packageResult.getPassedTests()}">
                <tr>
                  <td style="color:green;" colspan="5">
                    <tt>${passed_test.getFullName()}</tt>
                  </td>
                </tr>
              </j:forEach>
              <j:forEach var="failed_test" items="${packageResult.getFailedTests()}">
                <tr>
                  <td style="color:red;" colspan="5">
                    <tt>${failed_test.getFullName()}</tt>
                  </td>
                </tr>
              </j:forEach>
            </j:forEach>
          </j:forEach>
        </table>
		<br />
		<div>Note: See attachment also for summary report broken down by classes/methods.</div>
        <br />
      </div>
    </j:if>
 </body>
</j:jelly>

值得注意的是,在hudson/jenkins中,必须设置Publis JUnit test result report,同时,在editable email notification的Default Content中填入${JELLY_SCRIPT,template="myreport"},如下图所示:



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐