Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Junit test counts in Jenkins Pipeline project

I have just started with Jenkins

My freestyle project used to report JUnit tests results in Slack like this

MyJenkinsFreestyle - #79 Unstable after 4 min 59 sec (Open) Test Status:     Passed: 2482, Failed: 13, Skipped: 62 

Now I have moved the same to pipeline project, and all is good except that Slack notifications do not have Test Status

done MyPipelineProject #68 UNSTABLE 

I understand I have to construct the message to send to Slack, and I have done that above for now.

The only issue is how do I read the test status - the passed count, failed count etc. This is called "test summary" in Jenkins slack-plugin commit, and here is the screenshot testsummaryimage

So how do I access Junit tests count/details in Jenkins Pipeline project ? - so that these are reported in notifications.

UPDATE: In the Freestyle project, the Slack notification itself has the "test summary", and there is no option to opt (or not) for the test summary.

In Pipeline project, my "junit" command to "Publish JUnit test results" is before sending Slack notification.

So in code those lines look like this (this are last lines of the last stage):

bat runtests.bat junit 'junitreport/xml/TEST*.xml' slackSend channel: '#testschannel', color: 'normal', message: "done ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"; 
like image 457
vikramsjn Avatar asked Oct 07 '16 15:10

vikramsjn


People also ask

How do I get Jenkins pipeline results?

You get to the Test Result page by clicking a build link on the Status or History page of your Jenkins project, or by clicking Test Result in the menu on the left. Click the image to enlarge it. At the top of the page, Jenkins displays summary information about the executed tests and their total execution time.

Does Jenkins provide test report?

Jenkins understands the JUnit test report XML format (which is also used by TestNG). When this option is configured, Jenkins can provide useful information about test results, such as trends. The plugin also provides a generic API for other unit-test publisher plugins in Jenkins.


2 Answers

For anyone coming here in 2020, there appears to be a simpler way now. The call to 'junit testResults' returns a TestResultSummary object, which can be assigned to a variable and used later.

As an example to send the summary via slack:

def summary = junit testResults: '/somefolder/*-reports/TEST-*.xml' slackSend (    channel: "#mychannel",    color: '#007D00',    message: "\n *Test Summary* - ${summary.totalCount}, Failures: ${summary.failCount}, Skipped: ${summary.skipCount}, Passed: ${summary.passCount}" ) 
like image 135
Skylar Sutton Avatar answered Sep 17 '22 15:09

Skylar Sutton


From this presentation of Cloudbees I found that it should be possible via "build" object. It has code like

def testResult = build.testResultAction def total = testResult.totalCount 

But currentBuild does not provide access to testResultAction.

So kept searching and found this post "react on failed tests in pipeline script". There Robert Sandell has given "pro tip"

Pro tip, requires some "custom whitelisting":

AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class) if (testResultAction != null) {     echo "Tests: ${testResultAction.failCount} / ${testResultAction.failureDiffString} failures of ${testResultAction.totalCount}.\n\n"  } 

This worked like a charm - just that I had to deselect "Groovy sandbox" checkbox. Now I have these in the build log

Tests: 11  / ±0 failures of 2624 

Now I will use this to prepare string to notify in slack with test results.


UPDATE:

Finally, the function I used to get output like the following (Note the "failure diff" after failed tests is very useful)

Test Status:   Passed: 2628, Failed: 6  / ±0, Skipped: 0 

Is the following:

import hudson.tasks.test.AbstractTestResultAction  @NonCPS def testStatuses() {     def testStatus = ""     AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)     if (testResultAction != null) {         def total = testResultAction.totalCount         def failed = testResultAction.failCount         def skipped = testResultAction.skipCount         def passed = total - failed - skipped         testStatus = "Test Status:\n  Passed: ${passed}, Failed: ${failed} ${testResultAction.failureDiffString}, Skipped: ${skipped}"          if (failed == 0) {             currentBuild.result = 'SUCCESS'         }     }     return testStatus } 

UPDATE 2018-04-19

Note the above require manual "whitelisting" of methods used. Here is how you can whitelist all the methods in one go

Manually update the whitelist...

Exit Jenkins

Create/Update %USERPROFILE%.jenkins\scriptApproval.xml with the following content

<?xml version='1.0' encoding='UTF-8'?> <scriptApproval plugin="[email protected]"> <approvedScriptHashes> </approvedScriptHashes> <approvedSignatures> <string>method hudson.model.Actionable getAction java.lang.Class</string> <string>method hudson.model.Cause getShortDescription</string> <string>method hudson.model.Run getCauses</string> <string>method hudson.tasks.test.AbstractTestResultAction getFailCount</string> <string>method hudson.tasks.test.AbstractTestResultAction getFailureDiffString</string> <string>method hudson.tasks.test.AbstractTestResultAction getSkipCount</string> <string>method hudson.tasks.test.AbstractTestResultAction getTotalCount</string> <string>method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild</string> </approvedSignatures> <aclApprovedSignatures/> <approvedClasspathEntries/> <pendingScripts/> <pendingSignatures/> <pendingClasspathEntries/> </scriptApproval> 
  • Restart Jenkins
  • and then verify that the "In script approval" has the above entries approved
  • NOTE: Its the which is important. So if the scriptApproval file is already there, then you will generally need to ensure the contents of tag.
like image 30
vikramsjn Avatar answered Sep 20 '22 15:09

vikramsjn