Intellij Idea Gradle:使集成测试源模块工作

rsl1atfo  于 2023-02-15  发布在  其他
关注(0)|答案(1)|浏览(172)

我有一个简单的多模块Java Gradle项目。单元测试运行良好,但我无法为集成测试添加工作源集。测试已编译(我看到编译错误),并显示在控制台输出中,但未执行(现有测试应失败)。
此外,在IntelliJ IDEA中,源集被显示,但不是作为测试模块。我在测试旁边的排水沟中看到"运行"图标,但运行测试给出> No tests found for given includes: [de.cotto.integration_tests.moduletwo.ModuleTest.name](filter.includeTestsMatching)
我的问题:

  • 要运行测试(使用./gradlew build),需要进行哪些更改?
  • 我怎样才能让IntelliJ IDEA把源集作为一个"测试"模块?
  • 如何运行IntelliJ IDEA的测试?
  • 带模块的Java 14奖励积分(commit

源集合定义:

sourceSets {
    integrationTest {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

configurations {
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntimeOnly.extendsFrom runtimeOnly
}

task integrationTest(type: Test) {
    description = 'Runs integration tests.'
    group = 'verification'

    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test
}

check.dependsOn integrationTest
  • 分级6.4.1
  • J单元5
  • Java 14和11
  • 带或不带Java模块(JPMS、Jigsaw)
  • 智能J IDEA 2020.1.2
$ ./gradlew clean check --no-build-cache --console=plain
> Task :module-one:clean
> Task :module-two:clean
> Task :module-two:processResources NO-SOURCE
> Task :module-two:processTestResources NO-SOURCE
> Task :module-two:processIntegrationTestResources NO-SOURCE
> Task :module-one:compileJava
> Task :module-one:processResources NO-SOURCE
> Task :module-one:classes
> Task :module-two:compileJava
> Task :module-two:classes
> Task :module-one:compileTestJava
> Task :module-one:processTestResources NO-SOURCE
> Task :module-one:testClasses
> Task :module-two:compileTestJava
> Task :module-two:testClasses
> Task :module-two:compileIntegrationTestJava
> Task :module-two:integrationTestClasses
> Task :module-one:test
> Task :module-one:check
> Task :module-one:jar
> Task :module-two:test
> Task :module-two:integrationTest <-- What happens here? Why doesn't the test fail?
> Task :module-two:check

BUILD SUCCESSFUL in 2s
11 actionable tasks: 11 executed

源代码:https://github.com/C-Otto/gradle-integration-tests

i7uq4tfw

i7uq4tfw1#

缺少两个部分。以下内容仅为test模块定义了JUnit,而不是integrationTest。请参见https://discuss.gradle.org/t/integration-tests-not-being-run/28745

test {
    useJUnitPlatform()
}

因此,必须执行以下操作之一:

integrationTest {
    useJUnitPlatform()
}

tasks.withType(Test) {
    useJUnitPlatform()
}

此外,JUnit运行时不可用于集成测试,这可以通过包含测试运行时依赖项来解决:
使用integrationTestRuntimeOnly.extendsFrom testRuntimeOnly代替integrationTestRuntimeOnly.extendsFrom runtimeOnly
如果我添加这两个更改,测试将使用./gradlew check执行,我还可以从IntelliJ IDEA内部运行测试。

相关问题