【问题标题】:Share Java test helpers classes across different kinds of test modules in Gradle project在 Gradle 项目中跨不同类型的测试模块共享 Java 测试助手类
【发布时间】:2020-07-01 15:36:29
【问题描述】:

我有两种测试:单元测试和集成测试。我想有另一个我想在其中放置更复杂的测试用例。每种测试都放在不同的模块中。

下面我正在放置一个项目结构以使其更加明显

...
---buildSrc
------build.gradle(1)
------gradle.properties
---src
------main (files with production code)
------integration-test
------test (files with unit tests)
------new-test
------build.gradle(2)
...

我的目标: 我已经创建了很多测试助手,它们放在集成测试模块中,我想在新测试模块中重用它们。

这就是我在 build.gradle(2) 文件中的 sourceSets 的样子:

sourceSets {
    integrationTest {
        java.srcDir file('src/integration-test/java')
        resources.srcDir file('src/integration-test/resources')
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
    newTest {
        java {
            srcDirs 'src/new-test/java'
            srcDirs 'src/integration-test/java'
        }
        resources.srcDir file('/src/new-test/resources')
        compileClasspath += main.output + test.output + integrationTest.output
        runtimeClasspath += main.output + test.output + integrationTest.output
    }
}

感谢 (srcDirs 'src/integration-test/java') 我可以在 new-test 模块中使用来自集成测试的源代码,但我非常怀疑这是否是正确的方法,因为我我正在收到 Intellij 的通信:

Path [C:/Repositories/my-project/project/src/integration-test/java] of module [project.newTest] was removed from modules [project.integrationTest]

这是我在 build.gradle(2) 文件中的 newTest 任务:

task newTest(type: Test) {
    useJUnitPlatform() {
        excludeEngines "junit-vintage-engine"
    }
    testClassesDirs = sourceSets.newTest.output.classesDirs
    classpath = sourceSets.newtTest.runtimeClasspath
    outputs.upToDateWhen { false }
}

这是我在 build.gradle(2) 文件中的配置部分:

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime

    newTestCompile.extendsFrom testCompile
    newTestRuntime.extendsFrom testRuntime
}

我已经看过以下教程:

  1. Multi-project test dependencies with gradle (我只有一个项目,这里展示了如何分享测试 项目之间的类)
  2. How to share test classes across Gradle projects?
  3. http://michaelevans.org/blog/2019/09/21/stop-repeating-yourself-sharing-test-code-across-android-modules/(我目前无法迁移到 Gradle 5.6)
  4. Include tests from other module

那么我该怎么做才能在新测试模块中使用集成测试模块中的测试助手?我还想将这些助手类分离到单独的模块中,然后我的 new-test 和 integration-test 任务可以将其作为测试依赖项。

谢谢!

【问题讨论】:

    标签: testing gradle


    【解决方案1】:

    只需为测试助手/实用程序创建一个新项目(例如 test-lib)。将源代码放在 test-lib 项目中的 src/main/java 中,然后在需要的地方依赖 test-lib

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多