[英]Run unit tests in all projects, even if some fail
I have a multi module Gradle project. 我有一个多模块Gradle项目。 I want it to compile and do all other tasks as normal. 我希望它能够正常编译并执行所有其他任务。 But for unit tests, I want it to run all of them, instead of stopping as soon as one test in an early project fails. 但对于单元测试,我希望它能够运行所有这些,而不是在早期项目中的一个测试失败时立即停止。
I've tried adding 我试过添加
buildscript {
gradle.startParameter.continueOnFailure = true
}
which works for the tests, but also makes compile continue if something fails. 它适用于测试,但如果出现故障,也会继续编译。 That's not OK. 那不行。
Can I configure Gradle to continue, only for test tasks? 我可以配置Gradle继续,仅用于测试任务吗?
Try something like this in main build.gradle
and let me know, I have tested that with a small pmultiproject and seems to do what you need. 在main 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 ! ")
}
}
I changed @LazerBanana answer to cancel next tasks after test fail. 测试失败后,我更改了@LazerBanana的答案以取消下一个任务。
Usually all publishing starts after all tests (as an example - Artifactory plugin does this). 通常所有发布都在所有测试之后开始(作为示例 - Artifactory插件执行此操作)。 So, instead of build failure, it is better to add global task, which will be between tests and publishing (or run). 因此,最好是在测试和发布(或运行)之间添加全局任务,而不是构建失败。 So, your task sequence should be like this: 所以,你的任务序列应该是这样的:
Additional items: 附加项目:
Code: 码:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.