我在Jenkins管道中使用以下函数来处理单元测试结果并在Jenkins构建页面中显示它们:
def check_test_results(String path) {
step([
$class: 'XUnitBuilder',
testTimeMargin: '3000',
thresholdMode: 1,
thresholds: [
[$class: 'FailedThreshold', failureNewThreshold: '0', failureThreshold: '0', unstableNewThreshold: '', unstableThreshold: ''],
[$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '']
],
tools: [
[$class: 'JUnitType', deleteOutputFiles: true, failIfNotNew: false, pattern: path, skipNoTestFiles: false, stopProcessingIfError: true]
]
])
}
我知道J / Xunit结果显示在Jenkins构建页面中但我希望能够发送Slack通知(松弛通知已经配置并且正在工作),如果单元测试失败并且更重要的是,当它失败时,可能吗?
答案 0 :(得分:2)
您可以将try/catch
用于单元测试套件,但可能不单独使用。
def check_test_results(String path) {
try {
step([
$class: 'XUnitBuilder',
testTimeMargin: '3000',
thresholdMode: 1,
thresholds: [
[$class: 'FailedThreshold', failureNewThreshold: '0', failureThreshold: '0', unstableNewThreshold: '', unstableThreshold: ''],
[$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '']
],
tools: [
[$class: 'JUnitType', deleteOutputFiles: true, failIfNotNew: false, pattern: path, skipNoTestFiles: false, stopProcessingIfError: true]
]
])
}
catch(error) {
slackSend message: error
}
}
并根据自己的喜好自定义Slack通知。