我正在使用我的测试代码创建一个 JAR,并计划从不同的项目运行测试。
以下是我尝试实现的方法。
项目B:
configurations { testApi }
task testJar ....
artifacts { testApi testJar }
项目A:
dependencies {
testRuntime "xx.xxx.xxx.projectName", configuration: "integtest";
}
测试似乎没有使用这种方法运行。知道可能是什么问题吗?还有更好的方法吗?
最佳答案
Gradle 仅在通过相应 Test
类型任务的 testClassesDirs
属性配置的目录中查找测试类,该任务使用 java
插件用于 test
任务的 project.sourceSets.test.output.classesDirs
。不支持从依赖项中的 JAR 文件运行测试。
您需要提取 JAR 文件并将提取的目录添加到此属性。我认为类似以下构造的东西应该有效,但我没有测试它:
configurations {
externalTests
}
dependencies {
externalTests "xx.xxx.xxx.projectName", configuration: "integtest"
testRuntime "xx.xxx.xxx.projectName", configuration: "integtest"
}
test {
// if only one dependency in externalTests you can use the simpler, it will fail if there are multiple dependencies
testClassesDirs += zipTree(configurations.externalTests.singleFile)
// if multiple dependencies in externalTests you need to use
testClassesDirs += configurations.externalTests.files.collect { zipTree it }.sum()
}
关于java - 从 JAR 依赖项运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47791175/