如何在多项目中使用Spek设置Jacoco?

时间:2017-03-10 11:30:48

标签: unit-testing gradle kotlin jacoco

我想测量Jacoco在model项目中的覆盖率,这是一个子项目。 但是,在test任务之后,将跳过jacocoTestReport任务。

test

:model:compileKotlin UP-TO-DATE
:model:compileJava UP-TO-DATE
:model:copyMainKotlinClasses UP-TO-DATE
:model:processResources UP-TO-DATE
:model:classes UP-TO-DATE
:model:compileTestKotlin UP-TO-DATE
:model:compileTestJava UP-TO-DATE
:model:copyTestKotlinClasses UP-TO-DATE
:model:processTestResources UP-TO-DATE
:model:testClasses UP-TO-DATE
3 10, 2017 8:17:48 org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
Discovered TestEngines with IDs: [spek]
:model:junitPlatformTest
Test run finished after 113 ms
[         4 containers found      ]
[         0 containers skipped    ]
[         4 containers started    ]
[         0 containers aborted    ]
[         4 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         0 tests failed          ]
:model:test
:model:test SKIPPED

然后,输出xml文件。 (model / build / test-results / junit-platform / TEST-spek.xml)

jacocoTestReport

:model:compileKotlin UP-TO-DATE
:model:compileJava UP-TO-DATE
:model:copyMainKotlinClasses UP-TO-DATE
:model:processResources UP-TO-DATE
:model:classes UP-TO-DATE
:model:jacocoTestReport SKIPPED

的build.gradle:

buildscript {
    ext.kotlinVersion = '1.1.0'
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3"
    }
}

allprojects {
    ext {
        spekVersion = '1.1.0-beta3'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven { url "http://dl.bintray.com/jetbrains/spek" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":model") {
    apply plugin: "java"
    apply plugin: "kotlin"
    apply plugin: 'org.junit.platform.gradle.plugin'
    apply plugin: "jacoco"

    jacoco {
        reportsDir = file("$rootProject.buildDir/reports/jacoco")
    }

    jacocoTestReport {
        reports {
            html.enabled = true
            xml.enabled = true
            csv.enabled = false
        }
    }

    junitPlatform {
        filters {
            engines {
                include 'spek'
            }
        }
    }

    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
        testCompile "org.jetbrains.spek:spek-api:$spekVersion"
        testCompile 'org.amshove.kluent:kluent:1.14'
        testRuntime "org.jetbrains.spek:spek-junit-platform-engine:$spekVersion"
        testRuntime 'org.junit.platform:junit-platform-console:1.0.0-M3'
    }
}

如何让jacocoTestReport任务成功?

[编辑]

当我添加onlyIfjacocoTestReport任务运行时,它仍然会失败。

jacocoTestReport {
    onlyIf = {
        true
    }
}

输出:

:model:compileKotlin UP-TO-DATE
:model:compileJava UP-TO-DATE
:model:copyMainKotlinClasses UP-TO-DATE
:model:processResources UP-TO-DATE
:model:classes UP-TO-DATE
:model:jacocoTestReport FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':model:jacocoTestReport'.
> Unable to read execution data file model\build\jacoco\test.exec

1 个答案:

答案 0 :(得分:2)

默认情况下,JUnit Gradle插件会禁用标准的Gradle测试任务,但可以通过enableStandardTestTask标志覆盖此任务。似乎jacocoTestReport任务正在寻找它。

junitPlatform {
    filters {
        engines {
            include 'spek'
        }
    }

    enableStandardTestTask true
}

请参阅http://junit.org/junit5/docs/current/user-guide/#running-tests-build