我有一个多模块Gradle项目。我希望它能够正常编译并执行所有其他任务。但是对于单元测试,我希望它能够运行所有这些,而不是在早期项目中的一个测试失败时立即停止。
我已尝试添加
buildscript {
gradle.startParameter.continueOnFailure = true
}
适用于测试,但如果出现故障,也会继续编译。那不行。
我可以配置Gradle继续,仅用于测试任务吗?
答案 0 :(得分:1)
在主build.gradle
中尝试这样的事情让我知道,我已经用一个小的多项目进行了测试,似乎做了你需要的事情。
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 ! ")
}
}
答案 1 :(得分:1)
我改变了@LazerBanana的答案,在测试失败后取消了下一个任务。
通常所有发布都在所有测试之后开始(作为示例 - Artifactory插件执行此操作)。因此,最好是在测试和发布(或运行)之间添加全局任务,而不是构建失败。 所以,你的任务序列应该是这样的:
其他项目:
代码:
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)