2016-11-06 53 views
1

我使用的是詹金斯管道內的下列功能,以處理單元測試結果和在建詹金斯頁面顯示出來:如何在單元測試失敗時發送通知?

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

我知道到了焦耳/ xUnit的結果顯示在事實Jenkins構建頁面,但我希望能夠發送Slack通知(鬆散通知已配置並正在運行),如果單元測試失敗,更重要的是失敗時,這可能嗎?

回答

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通知。