testing - 在 Gradle 项目中跨不同类型的测试模块共享 Java 测试助手类

标签 testing gradle

我有两种测试:单元测试和集成测试。我想有另一个我想在其中放置更复杂的测试用例。每种测试都放在不同的模块中。
Bellow 我正在放置一个项目结构以使其更加明显

...
---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
}
我已经研究过以下教程:
  • Multi-project test dependencies with gradle (我只有一个项目,这里展示了如何共享测试
    项目之间的类)
  • How to share test classes across Gradle projects?
  • http://michaelevans.org/blog/2019/09/21/stop-repeating-yourself-sharing-test-code-across-android-modules/ (我目前无法迁移到 Gradle 5.6)
  • Include tests from other module

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

    最佳答案

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

    关于testing - 在 Gradle 项目中跨不同类型的测试模块共享 Java 测试助手类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62680362/

    相关文章:

    testing - 为什么 jest 期望匿名函数捕获错误?

    android - 停止使用互联网数据阻止Android Studio Gradle构建的方法

    java - 如何使用 Gradle 为测试和生产代码指定不同的(PMD、Checkstyle、Findbugs)规则集?

    java - 程序类型已经存在 com.google.gson.FieldAttributes

    android - 从GitHub导入Android项目并将其迁移到Gradle

    javascript - Typescript Pact.io 测试错误 : PopsicleError: Unable to connect to

    .net - 检查测试中的 Resharper 代码问题

    testing - 将 2 个数据集与 dbunit 进行比较?

    java - 并行运行testNG套件

    android - gradle中的 'compile …'是否需要安装设备中的Internet?