2012-07-04 17 views
10

Tôi đã tinh chỉnh mẫu thạch tiêu chuẩn để hiển thị kết quả thử nghiệm hiện tại trong bảng, tuy nhiên tôi thực sự muốn có thể hiển thị khác biệt được thấy trong trang kết quả kiểm tra của riêng Jenkins.Làm thế nào tôi có thể viết mẫu email mở rộng của Jenkins để hiển thị kết quả kiểm tra như báo cáo thử nghiệm tiêu chuẩn

Ví dụ:

JUnit Tests: 0 failures (±0) , 1 skipped (+1) 

Package    Duration Fail (diff) Skip (diff) Total (diff) 
foo.bar.baz    89 ms  0  0  1  +1  5  +2 

Trả lời

9

Viết mẫu Groovy cho Email Ext cắm thay vì Jelly mẫu. Trong mẫu Groovy bạn sẽ có quyền truy cập vào đối tượng Build cho bản dựng của mình. Sau đó, bạn có thể gọi số getTestResultAction để nhận số AbstractTestResultAction cho công trình mà bạn có thể truy vấn mọi thứ bạn cần.

Đây là liên kết đến Jenkins Main Module API. Mẫu Groovy mẫu cho plugin Ext Email có thể được tìm thấy trong $JENKINS_HOME/plugins/email-ext/WEB-INF/classes/hudson/plugins/emailext/templates/groovy-html.template. Thông tin thêm về cách sử dụng mẫu/tập lệnh Groovy có thể được tìm thấy trong Email Ext plugin documentation.

+0

@Jon, câu trả lời này có hữu ích không? –

+0

Tôi đã quản lý để kéo ra các bài kiểm tra thất bại bằng cách sử dụng build.testResultAction.failedTests, nhưng tôi không chắc chắn làm thế nào để truy cập tất cả các bài kiểm tra –

+1

Hình như truy cập PackageResult của phụ thuộc vào loại trả về bởi getTestResultAction - AggregatedTestResultAction & TestResultAction yêu cầu xử lý khác nhau. –

3

Nếu bạn đang đấu tranh về cách truy cập thông qua API nội bộ (khó biết và giới hạn tồn tại luôn), có một cách linh hoạt hơn để thực hiện.

Sử dụng FILE thẻ thay vì mẫu groovy

  1. sử dụng kịch bản để truy cập dữ liệu thử nghiệm của bạn qua Jenkins API, đối với trường hợp của bạn, nó cũng giống như http://jenkins.server/job/yourjob/lastCompletedBuild/testReport/api/xml và tạo ra file html của riêng bạn như email.html dưới workspace
  2. Ở dạng Default Content trong cấu hình email-mở rộng, sử dụng mã thông báo FILE để gửi email trực tiếp ${FILE, path="email.html"}

Ở bước 1 ở trên, bạn cũng có thể sử dụng cách linh hoạt hơn cho mẫu của riêng mình, tôi sử dụng tập lệnh python và mẫu chuỗi đơn giản.

Nó hoạt động hoàn hảo cho tôi.

+0

Bạn có muốn chia sẻ kịch bản mà bạn đã mô tả là bước 1 không? – callisto

+3

Nếu bạn sử dụng định dạng HTML, bạn sẽ cần sử dụng '$ {SCRIPT, template =" groovy-html.template "}' thay vì '$ {FILE}'. –

+0

Bạn có thể vui lòng xem chi tiết về điều này không. Tôi nghĩ rằng tôi cần phải làm điều tương tự. Tôi muốn hiển thị tin nhắn git cam kết hoàn chỉnh từ git trong email. Nhưng nó chỉ hiển thị dòng đầu tiên. Tôi nghĩ rằng tôi cần phải viết một số kịch bản .. – sharp

1

Giải pháp của tôi trong Jelly dựa trên kịch bản mặc định tĩnh-analysys.jelly

<!-- JUnit TEMPLATE --> 
    <j:set var="junitResultList" value="${it.JUnitTestResult}" /> 
    <j:if test="${junitResultList.isEmpty()!=true}"> 
    <div class="content"> 
     <a href="${rooturl}${build.url}/testReport"> 
     <h1>JUnit Tests</h1> 
     </a> 
     <table class="border"> 
     <tr> 
      <th class="border">Package</th> 
      <th class="border">Failed</th> 
      <th class="border">Failed (diff)</th> 
      <th class="border">Passed</th> 
      <th class="border">Passed (diff)</th> 
      <th class="border">Skipped</th> 
      <th class="border">Skipped (diff)</th> 
      <th class="border">Total</th> 
      <th class="border">Total (diff)</th> 
     </tr> 
     <j:forEach var="junitResult" items="${it.JUnitTestResult}"> 
      <j:forEach var="packageResult" items="${junitResult.getChildren()}"> 
      <tr> 
       <td class="border"> 
       <tt>${packageResult.getName()}</tt> 
       </td> 
       <td class="border test_failed">${packageResult.getFailCount()}</td> 
       <td class="border test_failed">${packageResult.getFailCount()-packageResult.previousResult.getFailCount()}</td> 
       <td class="border test_passed">${packageResult.getPassCount()}</td> 
       <td class="border test_passed">${packageResult.getPassCount()-packageResult.previousResult.getPassCount()}</td> 
       <td class="border test_skipped">${packageResult.getSkipCount()}</td> 
       <td class="border test_skipped">${packageResult.getSkipCount()-packageResult.previousResult.getSkipCount()}</td> 
       <td class="border"> 
       <b>${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()} 
       </b> 
       </td> 
       <td class="border"> 
       <b>${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()-packageResult.previousResult.getPassCount()-packageResult.previousResult.getFailCount()-packageResult.previousResult.getSkipCount()} 
       </b> 
       </td> 
      </tr> 
      <j:forEach var="failed_test" 
       items="${packageResult.getFailedTests()}"> 
       <tr> 
       <td class="test_failed" colspan="5"> 
        <tt>${failed_test.getFullName()}</tt> 
       </td> 
       </tr> 
      </j:forEach> 
      </j:forEach> 
     </j:forEach> 
     </table> 
     <br /> 
    </div> 
    </j:if> 
3

Để mở rộng về câu trả lời này: Viết mẫu Groovy cho Email Ext cắm thay vì Jelly mẫu. Trong nội dung thông báo Editable Email

  • kiểu nội dung thiết lập để "HTML" hoặc "Cả HTML và Plain Text"
  • và bao gồm các kịch bản hấp dẫn như thế này:

    $ {SCRIPT, template =" test.groovy "}

  • đặt tập lệnh groovy trong email-template home ví dụ /var/lib/jenkins/email-templates. xem bên dưới test.groovy.

Trong ví dụ dưới đây, mọi thử nghiệm được lặp lại bằng cách nhận từng đối tượng sau: '' 'junitResult.getChildren()' ''. Nếu bạn muốn lặp lại các phép thử không thành công thì có thể sử dụng junitResult.getFailedTests(). Xem hudson.tasks.junit.TestResult API: http://hudson-ci.org/javadoc/hudson/tasks/junit/PackageResult.html cũng thấy http://hudson-ci.org/javadoc/hudson/model/Build.html

Collection<ClassResult> getChildren() 
List<CaseResult> getFailedTests() 

Ví dụ/mẫu từ email-ext-plugin có thể được nhìn thấy ở đây: https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template

Ví dụ này cho thấy kết quả kiểm tra bản tóm tắt và bảng kết quả cho mỗi bộ kiểm tra và cá nhân kiểm tra. test.groovy:

<html> 
<body> 
<% 

    import hudson.model.* 

    def build = Thread.currentThread().executable 
    def buildNumber = build.number 
    def buildNumHash = build.getDisplayName() 

    def testCount = "0" 
    def testPassed = "0" 
    def testFailed = "0" 
    def testSkipped = "0" 
    def buildDuration = "0" 
    if(build.testResultAction) { 
     def testResult = build.testResultAction 
     testCount = String.format("%d",(testResult.totalCount)) 
     testPassed = String.format("%d",(testResult.result.passCount)) 
     testFailed = String.format("%d",(testResult.result.failCount)) 
     testSkipped = String.format("%d",(testResult.result.skipCount)) 
     testDuration = String.format("%.2f",(testResult.result.duration)) 
    } 

    def workspace = build.getEnvVars()["WORKSPACE"] 
    def buildName = build.getEnvVars()["JOB_NAME"] 
    def BUILD_STATUS = build.getEnvVars()["BUILD_STATUS"] 
    def BUILD_URL = build.getEnvVars()["BUILD_URL"] 

    def testResult = hudson.tasks.junit.TestResult 

    def testResult2 = build.getAction(hudson.tasks.junit.TestResultAction.class) 

%> 

start test.groovy <br><br> 
<b>TEST RESULT:</b> $testCount total, <b>$testPassed pass</b>, <b>$testFailed fail</b>, $testSkipped skip.<br> 
Workspace : $workspace<br> 
Project Name : $buildName $buildNumHash<br><br> 

<!-- GENERAL INFO --> 

<TABLE> 
    <TR><TD align="right"> 
    <j:choose> 
     <j:when test="${build.result=='SUCCESS'}"> 
     <IMG SRC="${rooturl}static/e59dfe28/images/32x32/blue.gif" /> 
     </j:when> 
      <j:when test="${build.result=='FAILURE'}"> 
     <IMG SRC="${rooturl}static/e59dfe28/images/32x32/red.gif" /> 
     </j:when> 
     <j:otherwise> 
     <IMG SRC="${rooturl}static/e59dfe28/images/32x32/yellow.gif" /> 
     </j:otherwise> 
    </j:choose> 
    </TD><TD valign="center"><B style="font-size: 200%;">BUILD ${build.result}</B></TD></TR> 
    <TR><TD>Build URL</TD><TD><A href="${rooturl}${build.url}">${rooturl}${build.url}</A></TD></TR> 
    <TR><TD>Project:</TD><TD>${project.name}</TD></TR> 
    <TR><TD>Date of build:</TD><TD>${it.timestampString}</TD></TR> 
    <TR><TD>Build duration:</TD><TD>${build.durationString}</TD></TR> 
    <TR><TD>Test duration:</TD><TD>${testDuration}</TD></TR> 
</TABLE> 
<BR/> 

<!-- JUnit TEMPLATE hudson.tasks.junit.TestResult --> 

<% 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 
} 
// API: http://hudson-ci.org/javadoc/hudson/tasks/junit/PackageResult.html 
%> 

<!-- JUnit TEMPLATE: all tests PASS FAIL SKIP > 
<% 
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"> <B>TEST SUITE: ${packageResult.getName()} Failed: ${packageResult.getFailCount()} test(s), Passed: ${packageResult.getPassCount()} test(s)</B>, Skipped: ${packageResult.getSkipCount()} test(s), Total: ${packageResult.getPassCount()+packageResult.getFailCount()+packageResult.getSkipCount()} test(s)</TD></TR> 
     <% packageResult.getChildren().each{ suite -> 
       suite.getChildren().each{ test -> 
      def colour = "lightgreen" 
      def highlight1="" 
      def highlight2="" 
      RESULT = test.getStatus() // FAILED or PASSED or SKIPPED 
      if (RESULT == hudson.tasks.junit.CaseResult.Status.FAILED || RESULT == hudson.tasks.junit.CaseResult.Status.REGRESSION) { 
       colour = "#ffcccc" 
       highlight1="<B>" 
       highlight2="</B>" 
      } 
      if (RESULT == hudson.tasks.junit.CaseResult.Status.SKIPPED) { colour = "#ffffb3" } 
     %> 
      <TR bgcolor="${colour}"><TD class="test" colspan="2">${highlight1}<li>${RESULT}: ${test.getFullName()} </li>${highlight2}</TD></TR> 
     <% } } 
     } 
} %> 
</TABLE> 
<BR/> 
<% 
} %> 

end of test.groovy 

</body> 
</html> 

ví dụ: đầu ra (văn bản không có màu/định dạng)

start test.groovy 

TEST RESULT: 18 total, 18 pass, 0 fail, 0 skip. 
Workspace : /var/lib/jenkins/jobs/jobname-1/workspace 
Project Name : jobname-1 #20 

BUILD SUCCESS 

Build URL http://jenkinsurl:port/job/jobname-1/20/ 
Project: jobname-1 
Date of build: Mon, 23 Jan 2017 09:29:00 +0000 
Build duration: 10 min 
Test duration: 267.12 

Test Results 
TEST SUITE: suitename1 Failed: 0 test(s), Passed: 3 test(s), Skipped: 0 test(s), Total: 3 test(s) 
* PASSED: suitename1.testclass.testname1 
* PASSED: suitename1.testclass.testname2 
* PASSED: suitename1.testclass.testname3 
TEST SUITE: suitename2 Failed: 2 test(s), Passed: 1 test(s), Skipped: 0 test(s), Total: 3 test(s) 
* PASSED: suitename2.testclass.testname1 
* FAILED: suitename2.testclass.testname2 
* REGRESSION: suitename2.testclass.testname3 

end of test.groovy 
Các vấn đề liên quan