Gradle集成测试构建但未运行

时间:2018-07-17 12:54:09

标签: gradle integration-testing junit5

我正在建立一个多模块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之后,我可以看到单元测试正在构建和运行,并且随后可以看到测试报告。我还可以看到,集成测试是在build dir中的类下面的单独integTest目录中进行构建的。我还可以看到在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.

1 个答案:

答案 0 :(得分:2)

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

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重新运行了该构建,并发现了以下内容,这些内容有助于我完善googlefu

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