2017-08-04 68 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) 
相关问题