【问题标题】:Integration tests in Gradle using Maven's naming conventions?使用 Maven 的命名约定在 Gradle 中进行集成测试?
【发布时间】:2019-10-31 14:25:31
【问题描述】:

来自 Maven,我正在探索 Gradle 作为替代方案。技术:Java 11、jUnit 5、Maven 3.6、Gradle 5.6。

我一直在配置集成测试。遵循 Maven 的 Surefire 和 Failsafe 插件的默认命名约定,我的测试位于标准测试目录中,并通过它们的后缀进行区分:单元测试以 Test.java 结尾,集成测试以 IT.java 结尾。

是否可以在 Gradle 中进行相同的设置?到目前为止,我已经看到了两种方法:

  • 使用 jUnit5 的标签(这意味着我必须去标记每个集成测试)
  • 为单元测试和集成测试使用单独的目录

理想情况下,我希望保持我的文件夹结构不变,因为它会影响多个 git 存储库。

【问题讨论】:

  • 基本上,您需要定义一个新的源集,排除以Test.java 结尾的文件,并配置名为test 的现有源集以排除以IT.java 结尾的文件。
  • 将您的test 任务配置为仅具有用于单元测试的后缀的include 类。然后创建另一个Test 任务(例如integrationTest),其中只有includes 带有用于集成测试的后缀的类。正如 Lukas 所指出的,多个源集也是一种选择。

标签: java maven gradle integration-testing


【解决方案1】:

好的,感谢LukasSlaw 的反馈,我想我设法让它与源集一起使用。

如果可以改进,请告诉我:

// define the dependencies of integrationTest, inherit from the unit test dependencies
configurations {
    integrationTestImplementation.extendsFrom(testImplementation)
    integrationTestRuntimeOnly.extendsFrom(testRuntimeOnly)
}

sourceSets {
    test {
        java {
            // exclude integration tests from the default test source set
            exclude "**/*IT.java"
        }
    }

    // new source set for integration tests
    integrationTest {
        // uses the main application code
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
        java {
            // the tests are at the same directory (or directories) as the unit tests
            srcDirs = sourceSets.test.java.srcDirs

            // exclude the unit tests from the integration tests source set
            exclude "**/*Test.java"
        }
        resources {
            // same resources directory (or directories) with the unit tests
            srcDirs = sourceSets.test.resources.srcDirs
        }
    }
}

// define the task for integration tests
task integrationTest(type: Test) {
    description = "Runs integration tests."
    group = "verification"
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test

    // I'm using jUnit 5
    useJUnitPlatform()
}

// make the check task depend on the integration tests
check.dependsOn integrationTest

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    相关资源
    最近更新 更多