Gradle 测试依赖性

发布于 2024-10-19 17:37:06 字数 129 浏览 9 评论 0原文

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

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

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

I have two projects, project A and Project B. Both are written in groovy and use gradle as their build system.

Project A requires project B.
This holds for both the compile and test code.

How can I configure that the test classes of project A have access to the test classes of project B?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

昇り龍 2024-10-26 17:37:07

对于 Gradle 1.5

task testJar(type: Jar, dependsOn: testClasses) {
    from sourceSets.test.java
    classifier "tests"
}

For Gradle 1.5

task testJar(type: Jar, dependsOn: testClasses) {
    from sourceSets.test.java
    classifier "tests"
}
混浊又暗下来 2024-10-26 17:37:07

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

dependencies {
  androidTestComplie project(path: ':ProjectA')
}

For Android on the latest gradle version (I'm currently on 2.14.1) you just need to add the below in Project B to get all the test dependencies from Project A.

dependencies {
  androidTestComplie project(path: ':ProjectA')
}
肤浅与狂妄 2024-10-26 17:37:07
dependencies {    
    testImplementation project(':project_name')
}
dependencies {    
    testImplementation project(':project_name')
}
绻影浮沉 2024-10-26 17:37:06

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

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

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')
}

This is for Java;我认为它也应该适用于 groovy。

You can expose the test classes via a 'tests' configuration and then define a testCompile dependency on that configuration.

I have this block for all java projects, which jars all test code:

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

configurations {
    tests
}

artifacts {
    tests testJar
}

Then when I have test code I want to access between projects I use

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

This is for Java; I'm assuming it should work for groovy as well.

醉殇 2024-10-26 17:37:06

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

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

这个问题有更多讨论:使用gradle进行多项目测试依赖

This is a simpler solution that doesn't require an intermediate jar file:

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

There's more discussion in this question: Multi-project test dependencies with gradle

凉栀 2024-10-26 17:37:06

现在 Gradle 中支持此功能(自 5.6 起)

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

  • 正确的依赖管理(实现/API)
  • 与测试代码良好分离(单独的源集)
  • 无需过滤掉测试类以仅公开
  • Gradle 维护的

实用程序示例:

:modul: one

modul/one/build.gradle

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

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

lazyly只需添加主实现配置的所有依赖项:

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 {}

:modul:other

modul/other/build.gradle

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

modul/other/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

This is now supported as a first class feature in Gradle (since 5.6)

Modules with java or java-library plugins can also include a java-test-fixtures plugin which exposes helper classes and resources to be consumed with testFixtures helper. Benefit of this approach against artifacts and classifiers are:

  • proper dependency management (implementation/api)
  • nice separation from test code (separate source set)
  • no need to filter out test classes to expose only utilities
  • maintained by Gradle

Example:

:modul:one

modul/one/build.gradle

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

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

or lazyly just add all dependencies of main implementation configuration:

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 {}

:modul:other

modul/other/build.gradle

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

modul/other/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
  }
}

For more info, see the documentation:
https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures

南笙 2024-10-26 17:37:06

这对我有用(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")

This works for me (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")
这个俗人 2024-10-26 17:37:06

上述解决方案有效,但不适用于最新版本的 Gradle 1.0-rc3。

     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
     }

The above solution works, but not for the latest version 1.0-rc3 of 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
     }
番薯 2024-10-26 17:37:06

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

dependencies {

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

}

:需要将 archives 命令添加到 ProjectA 的 build.gradle 中的 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罐子。然后可以将这个 -tests jar 添加为 ProjectB 的 testCompile 依赖项(如上所示)。

If ProjectA contains the test code you wish to use in ProjectB and ProjectB wants to use artifacts to include the test code, then ProjectB's build.gradle would look like this:

dependencies {

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

}

Then you need to add an archives command to the artifacts section in ProjectA's build.gradle:

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

configurations {
    tests
}

artifacts {
    tests testsJar
    archives testsJar
}

jar.finalizedBy(testsJar)

Now when ProjectA's artifacts are published to your artifactory they will include a -tests jar. This -tests jar can then be added as a testCompile dependency for ProjectB (as shown above).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文