在所有项目中运行单元测试,即使某些项目失败

Jor*_*orn 5 java unit-testing gradle

我有一个多模块 Gradle 项目。我希望它能够正常编译并执行所有其他任务。但对于单元测试,我希望它运行所有测试,而不是在早期项目中的一个测试失败后立即停止。

我尝试过添加

buildscript {
    gradle.startParameter.continueOnFailure = true
}
Run Code Online (Sandbox Code Playgroud)

这适用于测试,但如果出现失败,也可以使编译继续。那不行。

我可以将 Gradle 配置为仅针对测试任务继续吗?

Laz*_*ana 1

在 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 ! ")
    }

}
Run Code Online (Sandbox Code Playgroud)