2017-08-04 49 views
2

我有一個多模塊Gradle項目。我希望它能夠編譯並像平常一樣完成所有其他任務。但是對於單元測試,我希望它能夠運行所有這些測試,而不是在早期項目中的一個測試失敗時立即停止。在所有項目中運行單元測試,即使有些失敗

我已經嘗試添加

buildscript { 
    gradle.startParameter.continueOnFailure = true 
} 

其適用於測試,也使得編譯繼續,如果事情失敗。這不好。

我可以配置Gradle繼續嗎,僅用於測試任務嗎?

+0

會做的gradle'&&罐子的gradle test'工作? – flakes

+0

我會研究一下。也許'gradle test -continue'和另一個構建工件的步驟(例如'gradle jar')將會產生所需的效果。 – Jorn

+0

你能做到嗎Jorn? – LazerBanana

回答

1

嘗試這樣的主build.gradle這樣的事情,讓我知道,我已經測試了一個小的pmultiproject,似乎做你所需要的。

ext.testFailures = 0 //set a global variable to hold a number of failures 

gradle.taskGraph.whenReady { taskGraph -> 

    taskGraph.allTasks.each { task -> //get all tasks 
     if (task.name == "test") { //filter it to test tasks only 

      task.ignoreFailures = true //keepgoing if it fails 
      task.afterSuite { desc, result -> 
       if (desc.getParent() == null) { 
        ext.testFailures += result.getFailedTestCount() //count failures 
       } 
      } 
     } 
    } 
} 

gradle.buildFinished { //when it finishes check if there are any failures and blow up 

    if (ext.testFailures > 0) { 
     ant.fail("The build finished but ${ext.testFailures} tests failed - blowing up the build ! ") 
    } 

} 
+0

不,我仍然希望整個構建失敗,如果測試失敗。 – Jorn

+0

@Jorn我看到了,那麼你希望一切都能運行,但是測試失敗時構建失敗?我認爲這是默認實現的方式?如果有任何測試失敗,您仍然可以忽略失敗並實現測試偵聽器並且失敗構建。我會稍後更新答案。 – LazerBanana

+0

'那麼你希望一切都運行,但是當測試失敗時構建失敗。或多或少,是的。默認行爲是在任何任務失敗時立即停止構建。除了子模塊測試任務外,我想這樣做,這樣我就可以獲得儘可能多的信息。編譯失敗時,繼續下去是沒有意義的。當測試失敗時,運行更多測試是合理的,但不要部署工件。 – Jorn

1

我更改了@LazerBanana答案,以在測試失敗後取消下一個任務。

通常所有發佈都是在所有測試之後開始的(例如 - Artifactory插件會這樣做)。因此,不是構建失敗,而是添加全局任務,這將在測試和發佈(或運行)之間進行。 所以,你的任務序列應該是這樣的:

  1. 編譯每個項目
  2. 測試每個項目上的所有項目
  3. 收集測試結果和構建失敗
  4. 發佈的文物,通知用戶等。

附加項目:

  1. 我避免使用Ant Fail。有GradleException用於此目的
  2. testCheck任務執行doLast節的所有代碼,所推薦的gradle這個

代碼:

ext.testFailures = 0 //set a global variable to hold a number of failures 

task testCheck() { 
    doLast { 
     if (testFailures > 0) { 
      message = "The build finished but ${testFailures} tests failed - blowing up the build ! " 
      throw new GradleException(message) 
     } 
    } 
} 

gradle.taskGraph.whenReady { taskGraph -> 

    taskGraph.allTasks.each { task -> //get all tasks 
     if (task.name == "test") { //filter it to test tasks only 

      task.ignoreFailures = true //keepgoing if it fails 
      task.afterSuite { desc, result -> 
       if (desc.getParent() == null) { 
        ext.testFailures += result.getFailedTestCount() //count failures 
       } 
      } 

      testCheck.dependsOn(task) 
     } 
    } 
}  

// add below tasks, which are usually executed after tests 
// as en example, here are build and publishing, to prevent artifacts upload 
// after failed tests 
// so, you can execute the following line on your build server: 
// gradle artifactoryPublish 
// So, after failed tests publishing will cancelled 
build.dependsOn(testCheck) 
artifactoryPublish.dependsOn(testCheck) 
distZip.dependsOn(testCheck) 
configureDist.dependsOn(testCheck) 
相關問題