How to access Junit test counts in Jenkins Pipeline project

vikramsjn picture vikramsjn · Oct 7, 2016 · Viewed 23.8k times · Source

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>)";

Answer

Skylar Sutton picture Skylar Sutton · Jan 15, 2020

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}"
)