【问题标题】:Groovy email template is not printing the test resultsGroovy 电子邮件模板未打印测试结果
【发布时间】:2019-12-04 07:39:11
【问题描述】:

我是 groovy 的新手,我有一个 groovy 邮件模板,用于通过 jenkins 中的电子邮件插件发送带有构建摘要的邮件,当我对其进行测试时,它会打印有关构建的一般信息,而不是构建测试结果。谁能帮我解决这个问题?

我需要为此添加任何其他内容或下载任何内容吗?

下面是 groovy 脚本。

<head>
  <title>Build report</title>
  <style type="text/css">
    body
    {
      margin: 0px;
      padding: 15px;
    }

    body, td, th
    {
      font-family: "Lucida Grande", "Lucida Sans Unicode", Helvetica, Arial, Tahoma, sans-serif;
      font-size: 10pt;
    }

    th
    {
      text-align: left;
    }

    h1
    {
      margin-top: 0px;
    }

    li
    {
      line-height: 15pt;
    }

    .change-add
    {
      color: #272;
    }

    .change-delete
    {
      color: #722;
    }

    .change-edit
    {
      color: #247;
    }

    .grayed
    {
      color: #AAA;
    }

    .error
    {
      color: #A33;
    }

    pre.console
    {
      color: #333;
      font-family: "Lucida Console", "Courier New";
      padding: 5px;
      line-height: 15px;
      background-color: #EEE;
      border: 1px solid #DDD;
    }
  </style>
</head>
<body>

<h1>Build ${build.result}</h1>
<table>
  <tr><th>Build URL:</th><td><a href="${rooturl}${build.url}">${rooturl}${build.url}</a></td></tr>
  <tr><th>Project:</th><td>${project.name}</td></tr>
  <tr><th>Date of build:</th><td>${it.timestampString}</td></tr>
  <tr><th>Build duration:</th><td>${build.durationString}</td></tr>
</table>


<!-- ARTIFACTS -->
<% artifacts = build.artifacts
if (artifacts != null && artifacts.size() > 0) { %>
  <h2>Build artifacts</h2>
    <ul>
<%    artifacts.each() { f -> %>        
      <li><a href="${rooturl}${build.url}artifact/${f}">${f}</a></li>
<%    } %>
    </ul>
<% } %>

<% 
  testResult = build.testResultAction  

  if (testResult) {
    jobName = build.parent.name
    rootUrl = hudson.model.Hudson.instance.rootUrl
    testResultsUrl = "${rootUrl}${build.url}testReport/"

    lastBuildSuccessRate = String.format("%.2f", (testResult.totalCount - testResult.result.failCount) * 100f / testResult.totalCount)
    lastBuildDuration = String.format("%.2f", testResult.result.duration)

    startedPassing = []
    startedFailing = []
    failing = []

    previousFailedTestCases = new HashSet()
    currentFailedTestCase = new HashSet()

    if (build.previousBuild?.testResultAction) {
      build.previousBuild.testResultAction.failedTests.each {
        previousFailedTestCases << it.simpleName + "." + it.safeName
      }
    }

    testResult.failedTests.each { tr ->
        packageName = tr.packageName
        className = tr.simpleName
        testName = tr.safeName
        displayName = className + "." + testName

        currentFailedTestCase << displayName
        url = "${rootUrl}${build.url}testReport/$packageName/$className/$testName"
        if (tr.age == 1) {
          startedFailing << [displayName: displayName, url: url, age: 1]
        } else {
          failing << [displayName: displayName, url: url, age: tr.age]
        }
    }

    startedPassing = previousFailedTestCases - currentFailedTestCase
    startedFailing = startedFailing.sort {it.displayName}
    failing = failing.sort {it.displayName}
    startedPassing = startedPassing.sort()
%>

<% if (testResult) { %>
<h2>Test Results</h2>
<ul>
  <li>Total tests ran: <a href="${testResultsUrl}">${testResult.totalCount}</a></li>
  <li>Failure count and delta: ${testResult.failCount} ${testResult.failureDiffString}</li>
  <li>Success rate: ${lastBuildSuccessRate}% </li>
</ul> 
<% } %>

<% if (startedPassing) { %>
<h3>Following tests started passing. Good work!</h3>
<ul>
  <% startedPassing.each { %>
    <li>${it}</li>
  <% } %>
</ul>
<% } %>

<% if (startedFailing) { %>
<h3>Following tests started FAILING. Have the last change caused it!!</h3>
<ul>
  <% startedFailing.each { %>
    <li><a href="${it.url}">${it.displayName}</a></li>
  <% } %>
</ul>
<% } %>

<% if (failing) { %>
<h3>Following tests are conitnuously failing. Someone should look into it!!!</h3>
<ul>
  <% failing.each { %>
    <li><a href="${it.url}">${it.displayName}</a> (Failing since ${it.age} runs)</li>
  <% } %>
</ul>
<% } %>

<% } %>

<!-- BUILD FAILURE REASONS -->
<% if (build.result == hudson.model.Result.FAILURE) {
  log = build.getLog(100).join("\n")
  warningsResultActions = build.actions.findAll { it.class.simpleName == "WarningsResultAction" }

  if (warningsResultActions.size() > 0) { %>
    <h2>Build errors</h2>
    <ul>
    <% warningsResultActions.each {
        newWarnings = it.result.newWarnings
        if (newWarnings.size() > 0) {
          newWarnings.each {
            if (it.priority.toString() == "HIGH") { %>
              <li class="error">In <b>${it.fileName}</b> at line ${it.primaryLineNumber}: ${it.message}</li>
          <% }} %>
    <% }} %>
    </ul>
  <% } %>

<% } %>

</body>```

【问题讨论】:

    标签: jenkins groovy automation continuous-integration


    【解决方案1】:

    模板文件看起来不错。您需要先发布JUnit Test Result Reports,然后才能在模板中看到种子。

    1. 在您的工作中转到Post Build Action 并添加Publish JUnit Test Result Reports 并添加以下配置:

    提供您项目[maven项目]的target/surefire-reports目录中的所有.xml文件路径

    1. 构建项目并在电子邮件中检查结果(如果配置了模板),或者直接通过Template Testing 选项进行当前构建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多