0

How can I use import statements in Jenkinsfile?

This is the import statement:

import hudson.model.*
import jenkins.model.*
import hudson.tasks.test.AbstractTestResultAction

I want to use AbstractTestResultAction in a script section.

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • 1
    Please see similar problem described in that question: https://stackoverflow.com/questions/39920437/how-to-access-junit-test-counts-in-jenkins-pipeline-project – automatictester Jan 10 '19 at 15:52
  • Use import hudson.tasks.test.AbstractTestResultAction – Mike Jan 21 '19 at 17:16

1 Answers1

0

I solved it that way - this what the Jenkinsfile looks like:

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import hudson.tasks.test.AbstractTestResultAction;

@NonCPS
  def getTestSummary = { ->
    def testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
    def summary = ""

    if (testResultAction != null) {
        def total = testResultAction.getTotalCount()
        def failed = testResultAction.getFailCount()
        def skipped = testResultAction.getSkipCount()

        summary = "Test results:\n\t"
        summary = summary + ("Passed: " + (total - failed - skipped))
        summary = summary + (", Failed: " + failed + " ${testResultAction.failureDiffString}")
        summary = summary + (", Skipped: " + skipped)
    } else {
        summary = "No tests found"
    }
    return summary
  }

pipeline {

    ... 

 post {
      always {
          script {
              def testSummaryRaw = getTestSummary()
              def testSummary = "`${testSummaryRaw}`"
              ...
          }
      }
    }

    ... 

}
Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124