Reputation: 2199
I am trying to get my Jenkins to send a Slack message when the junit tests fail, but it does not do any other actions when the tests fail.
I am using a scripted pipeline in a Jenkinsfile in my Multibranch Pipeline
job.
Here is the testing stage in the pipeline:
node {
// setup and build Docker image
stage("Test") {
slackSend message: "Running tests!"
sh("docker run --rm -i \
-v '${env.WORKSPACE}/tests/results/':/tmp/tests/results \
${image} python3 manage.py test")
try {
slackSend message: "Checking tests!"
junit 'tests/results/*.xml'
} catch(err) {
slackSend message: "Error in tests!"
} finally {
slackSend message: "Finally in tests!"
}
}
}
I purposely added a test case that will fail. What I expect to see is all four Slack messages: Running tests!
, Checking tests!
, Error in tests!
, and Finally in tests!
, but all I see is the Running tests!
even though the Checking
message comes before the junit
line.
If the tests do not have any failures, then I see the Running
, Checking
and Finally
Slack messages, as I would expect.
Why doesn't the try/catch
execute when the junit tests fail?
Upvotes: 1
Views: 2717
Reputation: 66
junit pipeline command doesn't generate an exception, it only prepare/display unit tests report. In your case, "real unit tests failure" is during docker command execution, so you need simply check docker run return code. Plus check, that python3 command also returns error code when tests are failed.
Check this answer to figure out how to get docker run return code How to get the numeric exit status of an exited docker container?
Upvotes: 2