Dr.*_*rer 73 testing automated-tests build dependency-management gradle
我有两个项目,项目A和项目B.两者都是用groovy编写的,并使用gradle作为他们的构建系统.
项目A需要项目B.这适用于编译和测试代码.
如何配置项目A的测试类可以访问项目B的测试类?
Dav*_*ick 101
您可以通过"测试"配置公开测试类,然后在该配置上定义testCompile依赖项.
我有所有java项目的这个块,它包含所有测试代码:
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testJar
}
Run Code Online (Sandbox Code Playgroud)
然后,当我有测试代码时,我想访问我使用的项目
dependencies {
testCompile project(path: ':aProject', configuration: 'tests')
}
Run Code Online (Sandbox Code Playgroud)
这是针对Java的; 我认为它也适用于groovy.
Kip*_*Kip 16
这是一个更简单的解决方案,不需要中间jar文件:
dependencies {
...
testCompile project(':aProject').sourceSets.test.output
}
Run Code Online (Sandbox Code Playgroud)
在这个问题中有更多的讨论:使用gradle进行多项目测试依赖性
Dir*_*ann 11
现在,这已作为 Gradle 中的一流功能得到支持(自 5.6 起)
带有java插件的java-library模块还可以包含一个java-test-fixtures 插件,该插件公开要与 testFixtures 帮助程序一起使用的帮助程序类和资源。这种方法针对工件和分类器的好处是:
例子:
:模数:一
模块/一/build.gradle
plugins {
id "java-library" // or "java"
id "java-test-fixtures"
}
dependencies {
testFixturesImplementation("your.jar:dependency:0.0.1")
}
Run Code Online (Sandbox Code Playgroud)
或者lazyly只添加主配置的所有implementation依赖项:
val testFixturesImplementation by configurations.existing
val implementation by configurations.existing
testFixturesImplementation.get().extendsFrom(implementation.get())
Run Code Online (Sandbox Code Playgroud)
modul/one/src/ testFixtures /java/com/example/Helper.java
plugins {
id "java-library" // or "java"
id "java-test-fixtures"
}
dependencies {
testFixturesImplementation("your.jar:dependency:0.0.1")
}
Run Code Online (Sandbox Code Playgroud)
:模块:其他
模块/其他/build.gradle
plugins {
id "java" // or "java-library"
}
dependencies {
testImplementation(testFixtures(project(":modul:one")))
}
Run Code Online (Sandbox Code Playgroud)
模块/其他/src/test/java/com/example/other/SomeTest.java
val testFixturesImplementation by configurations.existing
val implementation by configurations.existing
testFixturesImplementation.get().extendsFrom(implementation.get())
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅文档: https://docs.gradle.org/current/userguide/java_testing.html#sec :java_test_fixtures
这对我有用(Java)
// use test classes from spring-common as dependency to tests of current module
testCompile files(this.project(':spring-common').sourceSets.test.output)
testCompile files(this.project(':spring-common').sourceSets.test.runtimeClasspath)
// filter dublicated dependency for IDEA export
def isClassesDependency(module) {
(module instanceof org.gradle.plugins.ide.idea.model.ModuleLibrary) && module.classes.iterator()[0].url.toString().contains(rootProject.name)
}
idea {
module {
iml.whenMerged { module ->
module.dependencies.removeAll(module.dependencies.grep{isClassesDependency(it)})
module.dependencies*.exported = true
}
}
}
.....
// and somewhere to include test classes
testRuntime project(":spring-common")
Run Code Online (Sandbox Code Playgroud)
上述解决方案有效,但不适用于最新版本1.0-rc3的Gradle.
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
// in the latest version of Gradle 1.0-rc3
// sourceSets.test.classes no longer works
// It has been replaced with
// sourceSets.test.output
from sourceSets.test.output
}
Run Code Online (Sandbox Code Playgroud)
如果 ProjectA 包含您希望在 ProjectB 中使用的测试代码,而 ProjectB 想要使用工件来包含测试代码,那么 ProjectB 的build.gradle将如下所示:
dependencies {
testCompile("com.example:projecta:1.0.0-SNAPSHOT:tests")
}
Run Code Online (Sandbox Code Playgroud)
然后需要在ProjectA的build.gradle中archives的artifactssection中添加一条命令:
task testsJar(type: Jar, dependsOn: testClasses) {
classifier = 'tests'
from sourceSets.test.output
}
configurations {
tests
}
artifacts {
tests testsJar
archives testsJar
}
jar.finalizedBy(testsJar)
Run Code Online (Sandbox Code Playgroud)
现在,当 ProjectA 的工件发布到您的工件时,它们将包含一个-tests jar。然后可以将此-tests jar 添加为 ProjectB 的 testCompile 依赖项(如上所示)。
| 归档时间: |
|
| 查看次数: |
42519 次 |
| 最近记录: |