react on failed tests in pipeline script

3,241 views
Skip to first unread message

domi

unread,
Mar 21, 2016, 5:49:08 AM3/21/16
to Jenkins Developers
Hi,

I’m trying to find a way how I can react on test failures. e.g. I run maven with the flag to ignore test failures so the job does not stop. But in case a test failure was discovered, I would like to send a mail or chat message - how can I do that?

sh 'mvn clean install -B -Dmaven.test.failure.ignore'
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/*.xml’])

if(testFailure) {
// send notification
}

regards Domi

Michael Neale

unread,
Mar 21, 2016, 10:16:53 PM3/21/16
to Jenkins Developers
I am assuming that ignoring test failures doesn't make the shell step fail? (Ie it returns a success code?)

If so, removing that and putting try/catch around it and then the archive step in the exception handler along with email could work. There may be a better way though (as that seems clumsy)

Andrew Bayer

unread,
Mar 21, 2016, 11:51:31 PM3/21/16
to jenkin...@googlegroups.com

I think the Junit result archiver should mark the build as unstable if there are failures...

On Mar 21, 2016 7:16 PM, "Michael Neale" <mne...@cloudbees.com> wrote:
I am assuming that ignoring test failures doesn't make the shell step fail? (Ie it returns a success code?)

If so, removing that and putting try/catch around it and then the archive step in the exception handler along with email could work. There may be a better way though (as that seems clumsy)

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/8617dfa6-1348-4a16-93c3-eff3dfca871a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Neale

unread,
Mar 21, 2016, 11:57:15 PM3/21/16
to Jenkins Developers
That would be preferable if so (in that case you don't want your shell step to error out, yes). 

Michael Neale

unread,
Mar 22, 2016, 12:03:12 AM3/22/16
to Jenkins Developers
Right but then without the step failure how can you trap the failure to send the failure email (seems clumsy to have to let the step fail, and then record archive results in the exception handler). 


On Tuesday, March 22, 2016 at 2:51:31 PM UTC+11, Andrew Bayer wrote:

Michael Neale

unread,
Mar 22, 2016, 12:12:47 AM3/22/16
to Jenkins Developers
OK sorry for the confusion, 

but you should be able to do what you do (so it doesn't fail on the sh step, but then marks the build as UNSTABLE in the junit archive step).
Then currentBuild.result should have the current build status (or another item in currentBuild, sorry am not sure). 

Robert Sandell

unread,
Mar 22, 2016, 4:43:00 AM3/22/16
to jenkin...@googlegroups.com
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"
}


For more options, visit https://groups.google.com/d/optout.



--
Robert Sandell
Software Engineer
CloudBees Inc.

domi

unread,
Mar 22, 2016, 4:47:45 AM3/22/16
to Jenkins Developers
Thanks!
For now I go with this (I hate whitelisting...):
    if(currentBuild.result != null && !"SUCCESS".equals(currentBuild.result)) {
        ...
    }

Jesse Glick

unread,
Mar 22, 2016, 12:22:57 PM3/22/16
to Jenkins Dev
On Tue, Mar 22, 2016 at 4:47 AM, domi <do...@fortysix.ch> wrote:
> if(currentBuild.result != null &&
> !"SUCCESS".equals(currentBuild.result)) {
> ...
> }

Yes that is the current recommendation.
Reply all
Reply to author
Forward
0 new messages