英文:
How to fail gradle build when no tests where executed/found?
问题
有没有一种方法可以在没有找到或执行测试时使构建失败。
例如,当未定义useJUnitPlatform并且存在测试类时。即使没有执行任何测试,构建也会成功完成。在这种情况下,我希望构建失败。我还尝试设置failOnNoMatchingTests,但它没有起作用...
test {
// useJUnitPlatform()
getFilter().failOnNoMatchingTests(true)
}
有人有解决这个问题的方法吗?
英文:
Is there a way to fail the build when no tests where found or executed.
e.g. when useJUnitPlatform is not defined and test classes exists. The build finish successfully even if no tests are executed. In this case i would like that the build fails. I also tried to set failOnNoMatchingTests but it didn't work...
test {
// useJUnitPlatform()
getFilter().failOnNoMatchingTests(true)
}
Does sombody has a solution for this problem?
答案1
得分: 0
你可以尝试使用 [afterSuite][1] 来实现这个,以下代码对我有效 -
test {
//useJUnitPlatform()
testLogging {
afterSuite { desc, result ->
if(result.testCount == 0)
throw new GradleException("未执行任何测试。构建失败。")
}
}
}
由于这里没有执行 `useJunitPlatform()`,你会看到抛出 GradleException。我修改了这里的原始解决方案 - [打印 gradle 测试][2]
[1]: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:afterSuite(groovy.lang.Closure)
[2]: https://gist.github.com/orip/4951642
英文:
You can try using afterSuite (afterSuite) for this, the following worked for me -
test {
//useJUnitPlatform()
testLogging {
afterSuite { desc, result ->
if(result.testCount == 0)
throw new GradleException("No tests were executed. Build failed.")
}
}
}
As useJunitPlatform()
is not executed here, you will see the GradleException being thrown. I modified the original solution from here - Print gradle tests
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论