85

我有两个项目,项目 A 和项目 B。两者都是用 groovy 编写的,并使用 gradle 作为构建系统。

项目 A 需要项目 B。这适用于编译和测试代码。

如何配置项目 A 的测试类可以访问项目 B 的测试类?

4

9 回答 9

108

您可以通过“测试”配置公开测试类,然后在该配置上定义 testCompile 依赖项。

我对所有 java 项目都有这个块,它包含所有测试代码:

task testJar(type: Jar, dependsOn: testClasses) {
    baseName = "test-${project.archivesBaseName}"
    from sourceSets.test.output
}

configurations {
    tests
}

artifacts {
    tests testJar
}

然后,当我有测试代码时,我想在我使用的项目之间访问

dependencies {
    testCompile project(path: ':aProject', configuration: 'tests')
}

这是针对 Java 的;我假设它也应该适用于 groovy。

于 2011-03-01T10:20:52.450 回答
18

这是一个更简单的解决方案,不需要中间 jar 文件:

dependencies {
  ...
  testCompile project(':aProject').sourceSets.test.output
}

这个问题有更多的讨论:Multi-project test dependencies with gradle

于 2015-08-18T21:45:28.273 回答
8

这对我有用(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")
于 2012-10-30T14:07:05.767 回答
6

现在支持作为 Gradle 中的一流功能(自 5.6 起)

带有javajava-library插件的模块还可以包含一个java-test-fixtures 插件,该插件公开了要与 testFixtures 助手一起使用的助手类和资源。这种方法对工件和分类器的好处是:

  • 适当的依赖管理(实现/api)
  • 与测试代码很好的分离(单独的源集)
  • 无需过滤掉测试类以仅公开实用程序
  • 由 Gradle 维护

例子:

:模块:一个

模块/一个/build.gradle

plugins {
  id "java-library" // or "java"
  id "java-test-fixtures"
}

dependencies {
  testFixturesImplementation("your.jar:dependency:0.0.1")
}

或者lazyly只是添加主配置的所有依赖项:implementation

val testFixturesImplementation by configurations.existing
val implementation by configurations.existing
testFixturesImplementation.get().extendsFrom(implementation.get())

modul/one/src/ testFixtures /java/com/example/Helper.java

package com.example;
public class Helper {}

:模块:其他

模块/其他/build.gradle

plugins {
  id "java" // or "java-library"
}
dependencies {
  testImplementation(testFixtures(project(":modul:one")))
}

模块/其他/src/test/java/com/example/other/SomeTest.java

package com.example.other;
import com.example.Helper;
public class SomeTest {
  @Test void f() {
    new Helper(); // used from :modul:one's testFixtures
  }
}

有关更多信息,请参阅文档: https ://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures

于 2021-02-10T14:18:21.157 回答
5

上述解决方案有效,但不适用于最新版本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
     }
于 2012-05-31T14:22:30.800 回答
5

如果 ProjectA 包含您希望在 ProjectB 中使用的测试代码,并且 ProjectB 想要使用工件来包含测试代码,那么 ProjectB 的build.gradle将如下所示:

dependencies {

  testCompile("com.example:projecta:1.0.0-SNAPSHOT:tests")

}

然后你需要在 ProjectA 的 build.gradle 部分中添加一个archives命令:artifacts

task testsJar(type: Jar, dependsOn: testClasses) {
    classifier = 'tests'
    from sourceSets.test.output
}

configurations {
    tests
}

artifacts {
    tests testsJar
    archives testsJar
}

jar.finalizedBy(testsJar)

现在,当 ProjectA 的工件发布到您的工件时,它们将包含一个-tests jar。然后可以将此-tests jar 添加为 ProjectB 的 testCompile 依赖项(如上所示)。

于 2019-02-11T23:41:53.623 回答
0

对于 Gradle1.5

task testJar(type: Jar, dependsOn: testClasses) {
    from sourceSets.test.java
    classifier "tests"
}
于 2015-07-23T15:54:10.613 回答
0

对于最新 gradle 版本(我目前在 2.14.1)上的 Android,您只需在项目 B 中添加以下内容即可从项目 A 中获取所有测试依赖项。

dependencies {
  androidTestComplie project(path: ':ProjectA')
}
于 2016-11-01T17:33:03.150 回答
0
dependencies {    
    testImplementation project(':project_name')
}
于 2021-05-22T03:13:14.507 回答