我试图让我的 Jenkins 在 junit 测试失败时发送 Slack 消息,但在测试失败时它不会执行任何其他操作。 我在Multibranch Pipeline
工作中在 Jenkinsfile 中使用脚本化管道。
以下是管道中的测试阶段:
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!"
}
}
}
我特意添加了一个将失败的测试用例。 我希望看到的是所有四个 Slack 消息:Running tests!
、Checking tests!
、Error in tests!
和Finally in tests!
,但我看到的只是Running tests!
,即使Checking
消息出现在junit
行之前。
如果测试没有任何失败,那么我会看到Running
,Checking
和Finally
Slack消息,正如我所期望的那样。
为什么当 junit 测试失败时try/catch
不执行?
junit 管道命令不会生成异常,它只会准备/显示单元测试报告。在您的情况下,"真正的单元测试失败"是在 docker 命令执行期间,因此您只需检查 docker 运行返回代码即可。另外检查,当测试失败时,python3命令也会返回错误代码。
检查此答案以弄清楚如何获取 docker 运行返回代码 如何获取已退出的码头工人容器的数字退出状态?