如何在没有执行/找到测试时使Gradle构建失败?

huangapple go评论140阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年8月10日 14:17:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定