首页 > 解决方案 > 使用 Maven 的命名约定在 Gradle 中进行集成测试?

问题描述

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

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

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

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

标签: javamavengradleintegration-testing

解决方案


好的,感谢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

推荐阅读