0

I'm using groovy in jenkins pipeline scripts. How i can take the results of falling unit tests from junit report file? Now i'm trying to get them by code:

import hudson.tasks.junit.TestResultAction
import hudson.model.*
import java.*
import java.lang.*
import hudson.*
import hudson.model.*
import jenkins.model.Jenkins

...some code ...

dir (workspace){                 
    step([$class: 'JUnitResultArchiver', keepLongStdio: true, testResults: "artifacts/*/*.xml"])
    result_junit = count_fail_unit_tests()
    echo "UNIT TESTS FAIL COUNT: ${result_junit}"                     
    if (result_junit != 0){
            currentBuild.displayName = "UNIT TESTS FAIL"
        }
    throw new Exception("warning_exception")
    }
archiveArtifacts allowEmptyArchive: true, artifacts: "log/*", caseSensitive: false
}


@NonCPS
def count_fail_unit_tests() {           
    def count_fail_tests = manager.build.getAction(hudson.tasks.junit.TestResultAction.class).getFailCount()
    return count_fail_tests
}

and got an error

ERROR: groovy.lang.MissingPropertyException: No such property: manager for class: Script4

Earlier i got the count of errors by code:

dir (workspace){
    result_junit = junit keepLongStdio: true, testResults: "artifacts/*/*.xml"
    echo "UNIT TESTS FAIL COUNT: ${result_junit}"                     
    if (result_junit != 0){
            currentBuild.displayName = "UNIT TESTS FAIL"
        }
    throw new Exception("warning_exception")
    }
archiveArtifacts allowEmptyArchive: true, artifacts: "log/*", caseSensitive: false
}

and it was working, but it had bug: if use junit keepLongStdio: true, testResults: "artifacts/*/*.xml" and report .xml file has incorrect structure, building not failing and stay in status "SUCCESS". With using step([$class: 'JUnitResultArchiver', keepLongStdio: true, testResults: "artifacts/*/*.xml"]) it fails, thats why i want to use this metod. If i try

some_return = step([$class: 'JUnitResultArchiver', keepLongStdio: true, testResults: "artifacts/*/*.xml"]) 
echo "${some_return}"

get

null

Is there some metods for junit keepLongStdio: true, testResults: "artifacts/*/*.xml" to fail build, if xml junit report is incorrect? Or how i can take counts from

step([$class: 'JUnitResultArchiver', keepLongStdio: true, testResults: "artifacts/*/*.xml"])
gigteg
  • 1
  • 2
  • Try to use `currentBuild.rawBuild` in place of `manager.build`. – zett42 Mar 18 '20 at 08:42
  • try this and got another error: [ERROR: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.tasks.test.AbstractTestResultAction getFailCount] – gigteg Mar 24 '20 at 14:08
  • @zett42 Thanks for answer, i tried this `currentBuild.rawBuild.getAction(hudson.tasks.junit.TestResultAction.class).getFailCount();` and got another error: [ERROR: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.tasks.test.AbstractTestResultAction getFailCount] – gigteg Mar 24 '20 at 15:18
  • https://stackoverflow.com/q/38276341/7571258 – zett42 Mar 24 '20 at 16:07

0 Answers0