51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

英文:

How to fail gradle build when no tests where executed/found?

问题 {#heading}

有没有一种方法可以在没有找到或执行测试时使构建失败。

例如,当未定义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 {#1}

得分: 0

你可以尝试使用 [afterSuite][1] 来实现这个,以下代码对我有效 -

test {
//useJUnitPlatform()
testLogging {
afterSuite { desc, result ->
if(result.testCount == 0)
throw new GradleException("未执行任何测试。构建失败。")
}
}
}

由于这里没有执行 `useJunitPlatform()`,你会看到抛出 GradleException。我修改了这里的原始解决方案 - [打印 gradle 测试][2]

英文:

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


赞(0)
未经允许不得转载:工具盒子 » 如何在没有执行/找到测试时使Gradle构建失败?