swami
swami

Reputation: 11

How to access Junit test results of downstream pipeline job

I am trying to use import hudson.tasks.junit.TestResult to get the count of Junit tests from a downstream job and unable to get count.

Expected: Able to pull count from downstream.

Actual: testResultaction is null always.

stage ('Starting Smoke Check') {
    steps{
        script {
            echo 'Staring Health Check'
            def jobBuild = build job:'JI',parameters:[]
            def jobResult = jobBuild.getResult()
            echo "Build returned result: ${jobResult}"
            def log = jobBuild.rawBuild.log
            echo "===================START LOG==================="
            println("Build log: ${log}")

            TestResult testResultAction =  jobBuild.rawBuild.getAction(TestResult.class)
            println "TestResult Action: ${testResultAction}"
            if (testResultAction != null) {
               def totalNumberOfTests = testResultAction.getTotalCount()
               def failedNumberOfTests = testResultAction.getFailCount()
               def skippedNumberOfTests = testResultAction.getSkipCount()
               def passedNumberOfTests = totalNumberOfTests - failedNumberOfTests - skippedNumberOfTests                        
               echo "Tests Report:\n Passed: ${passedNumberOfTests}; Failed: ${failedNumberOfTests} ${failedDiff}; Skipped: ${skippedNumberOfTests}  out of ${totalNumberOfTests} "
            } 
            echo 'Health Check completed successfully!!'
        }
    }
}

Upvotes: 1

Views: 1632

Answers (1)

ludenus
ludenus

Reputation: 1341

worked for me after I modified your code by specifying full class name in getAction(hudson.tasks.junit.TestResultAction.class)

script {
               def runWrapper = build(
                   job: 'testjobs/qa-tmp-2',
                   parameters: [],
                   propagate: false,
                )

                def jobResult = runWrapper.getResult()
                echo "Build returned result: ${jobResult}"

                def testResultAction =  runWrapper.rawBuild.getAction(hudson.tasks.junit.TestResultAction.class)
                echo "testResultAction ${testResultAction}"

                if (testResultAction != null) {
                  def totalNumberOfTests = testResultAction.getTotalCount()
                  def failedNumberOfTests = testResultAction.getFailCount()
                  def skippedNumberOfTests = testResultAction.getSkipCount()
                  def passedNumberOfTests = totalNumberOfTests - failedNumberOfTests - skippedNumberOfTests                        
                  echo "Tests Report:\n Passed: ${passedNumberOfTests}; Failed: ${failedNumberOfTests}; Skipped: ${skippedNumberOfTests}  out of ${totalNumberOfTests} "
                }
            }

Upvotes: 1

Related Questions