如何访问下游管道作业的Junit测试结果

时间:2019-06-03 08:20:27

标签: jenkins groovy jenkins-pipeline test-results

我正在尝试使用import hudson.tasks.junit.TestResult从下游作业中获取Junit测试的计数,但无法获得计数。

预期:能够从下游提取计数。

实际:testResultaction始终为空。

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!!'
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在我通过在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} "
                }
            }