How can I write a Jenkins email-ext template to display test results like the standard test report

后端 未结 5 1398
暗喜
暗喜 2020-12-23 22:27

I have tweaked the standard jelly template to display the current test results in a table, however I really want to be able to display diffs as seen in Jenkins own test resu

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 23:17

    To expand on this answer: Write a Groovy template for Email Ext plugin instead of Jelly template. In Editable Email Notification content

    • set content type to "HTML" or "Both HTML and Plain Text"
    • and include the groovy script like this:

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

    • put the groovy script in email-templates home e.g. /var/lib/jenkins/email-templates. see below test.groovy.

    In the example below every test is iterated by getting each of these objects: '''junitResult.getChildren()'''. If one desired to iterate only failed tests then junitResult.getFailedTests() could be used. See the hudson.tasks.junit.TestResult API: http://hudson-ci.org/javadoc/hudson/tasks/junit/PackageResult.html also see http://hudson-ci.org/javadoc/hudson/model/Build.html

    Collection getChildren()
    List    getFailedTests()
    

    Example/template from email-ext-plugin can be seen here: https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template

    This example shows summary test result and table for results for each test suite and individual test. test.groovy:

    
    
    <%
    
        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 

    TEST RESULT: $testCount total, $testPassed pass, $testFailed fail, $testSkipped skip.
    Workspace : $workspace
    Project Name : $buildName $buildNumHash

    BUILD ${build.result}
    Build URL${rooturl}${build.url}
    Project:${project.name}
    Date of build:${it.timestampString}
    Build duration:${build.durationString}
    Test duration:${testDuration}

    <% 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 %>
    热议问题