Gradle集成测试构建但未运行

8
我正在设置一个多模块Gradle项目,并尝试为所有子项目分离集成测试。我成功创建了integTest任务,我可以看到它构建了集成测试类并在不同的目录中创建了集成测试报告,而不是单元测试。但它没有运行我的集成测试。
项目结构大致如下:
|_ gradle
| |_ integration-test.gradle
|_ subproject
| |_ src
| |  |_ main
| |  | |_ java
| |  | |_ resources
| |  |_ test
| |  | |_ java
| |  | |_ resources
| |  |_ integTest
| |    |_ java
| |    | |_sit
| |    |   |_ MyTest.java
| |    |_ resources
| |_ build.gradle
|_ build.gradle
|_ gradle.properties
|_ settings.gradle

在 integration-test.gradle 文件中,我有以下内容:
sourceSets {
    integTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
        compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
        runtimeClasspath += output + compileClasspath
    }
}

dependencies {
    integTestCompile sourceSets.main.output
    integTestCompile sourceSets.test.output 

    integTestCompile configurations.compile
    integTestCompile configurations.testCompile 

    integTestRuntime configurations.runtime
    integTestRuntime configurations.testRuntime 
}

task integTest(type: Test) {
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = 'Runs the integration tests.' 

    maxHeapSize = '1024m' 

    testClassesDir = sourceSets.integTest.output.classesDir
    classpath = sourceSets.integTest.runtimeClasspath 

    binResultsDir = file("$buildDir/integration-test-results/binary/integTest")

    reports { 
        html.destination = "$buildDir/reports/integration-test"
        junitXml.destination = "$buildDir/integration-test-results"
    }

    mustRunAfter tasks.test
}

check.dependsOn integTest

然后,在子项目的build.gradle文件的顶部添加以下内容:

apply from: "$rootDir/gradle/integration-test.gradle"

在运行gradle clean build后,我可以看到单元测试正在构建和运行,并在之后查看测试报告。我也可以看到集成测试正在一个名为integTest的目录下面构建,该目录在build目录中的classes目录下面。我还可以看到在build目录中创建了一个新的integration-test-results目录,但是在MyTest.java中定义的测试没有运行。
当使用-i标志运行时,我也可以在控制台中看到这个情况。
> Task :javaproject:integTest
Task ':javaproject:integTest' is not up-to-date because:
  Output property 'binResultsDir' file E:\dev\java\workspace\javaproject\subproject\build\integration-test-results\binary\integTest has been removed.
  Output property 'binResultsDir' file E:\dev\java\workspace\javaproject\subproject\build\integration-test-results\binary\integTest\output.bin has been removed.
  Output property 'binResultsDir' file E:\dev\java\workspace\javaproject\subproject\build\integration-test-results\binary\integTest\output.bin.idx has been removed.
Finished generating test XML results (0.0 secs) into: E:\dev\java\workspace\javaproject\subproject\build\integration-test-results
Generating HTML test report...
Finished generating test html results (0.006 secs) into: E:\dev\java\workspace\javaproject\subproject\build\reports\integration-test
:javaproject:integTest (Thread[Task worker for ':',5,main]) completed. Took 0.092 secs.
:javaproject:check (Thread[Task worker for ':',5,main]) started.

你在这里发布的所有内容都表明Gradle正在按预期工作。你确定问题不是出在测试本身上吗?你能手动运行测试吗? - user31601
@user31601 谢谢,我找到答案了! - PDStat
1个回答

26

对于使用JUnit5并以此方式设置集成测试的任何人,请确保将useJUnitPlatform()添加到您的集成测试任务中。所以我的任务变成了:

Right for anyone using JUnit5 and setting up integration tests in this way make sure to add useJUnitPlatform() to your integration test task. So my task became

task integTest(type: Test) {
    useJUnitPlatform()
    group = LifecycleBasePlugin.VERIFICATION_GROUP
    description = 'Runs the integration tests.' 

    maxHeapSize = '1024m' 

    testClassesDir = sourceSets.integTest.output.classesDir
    classpath = sourceSets.integTest.runtimeClasspath 

    binResultsDir = file("$buildDir/integration-test-results/binary/integTest")

    reports { 
        html.destination = "$buildDir/reports/integration-test"
        junitXml.destination = "$buildDir/integration-test-results"
    }

    mustRunAfter tasks.test
}

然后,一切都神奇地开始工作了。我注意到这一点,因为我使用 gradle clean build -d 重新运行了构建,并发现了以下内容,这帮助我改进了我的谷歌搜索技巧。

13:58:08.909 [DEBUG] [TestEventLogger] Gradle Test Run :subproject:integTest STARTED
13:58:08.943 [DEBUG] [org.gradle.api.internal.tasks.testing.detection.AbstractTestFrameworkDetector] test-class-scan : failed to scan parent class java/lang/Object, could not find the class file

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接