【发布时间】:2021-05-19 13:20:36
【问题描述】:
我们可以运行 junit batchtest with tests picked from a jar file.
<zipfileset src="tests-only.jar" includes="**/*Test.class"/>
这会运行从 test-only.jar 中挑选的所有匹配测试
如何用 gradle 做类似的事情。 testClassesDir 只需要实际目录。
【问题讨论】:
我们可以运行 junit batchtest with tests picked from a jar file.
<zipfileset src="tests-only.jar" includes="**/*Test.class"/>
这会运行从 test-only.jar 中挑选的所有匹配测试
如何用 gradle 做类似的事情。 testClassesDir 只需要实际目录。
【问题讨论】:
Gradle 没有用于运行 Jar 文件中包含的测试类的内置功能。 (请随时在http://forums.gradle.org 提交功能请求。)应该做的是解压Jar(作为一个单独的任务),然后相应地设置testClassesDir。请注意,测试类也需要出现在测试任务的classpath 上(但为此使用 Jar 可能就足够了)。
【讨论】:
请参阅 Gradle 用户名的 23.12.2 部分。请注意,此信息来自this question
【讨论】:
可以将 jar 文件视为 zip 文件并结合自定义 gradle 测试任务。
让我们假设 jar-File 位于您的项目主 runtimeClasspath 上,并命名为“tests-only.jar”。
下面的 sn-p 允许 gradle 在 jar-File 中查找类,而无需实际提取它们:
task testOnlyTests(type: Test) {
useJUnit()
// the project runtimeClasspath should include all dependencies of the tests-only.jar file
classpath = project.sourceSets.main.runtimeClasspath
testClassesDirs = project.sourceSets.main.output
// find file tests-only.jar in dependencies or adjust the snippet to find it by other means
File testJar = classpath.files.find { it.toString().contains("tests-only.jar") }
testClassesDirs += zipTree(testJar)
include("**/*Test.class")
}
同样也应该直接适用于您项目的“测试”配置。
【讨论】: