Gradle 找不到测试

Gradle can't find tests

我正在为我的 Kotlin 项目使用 Spek 测试框架。我可以 运行 通过 Intellij Idea Spek 插件进行测试,但不能 运行 通过 gradle (构建或测试)。根据 SimpleTest.kt 当来自 Idea 插件的 运行ning 1 次测试成功和 1 次失败时,当 运行ning 通过 gradle 时它说找到 1 个容器有 0 次测试。如何通过 gradle?

设置启动测试

我的 gradle 和测试文件:

build.gradle:

buildscript {
    ext.kotlin_version = '1.1.4-2'
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4"
    }
}

apply plugin: "idea"
apply plugin: "java"
apply plugin: "kotlin"
apply plugin: "application"
apply plugin: "org.junit.platform.gradle.plugin"

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

mainClassName = "app.MainKt"

repositories {
    mavenCentral()
    jcenter()
    maven { url "http://dl.bintray.com/jetbrains/spek" }
}

dependencies {
    testCompile 'org.jetbrains.kotlin:kotlin-test'
    testCompile 'org.jetbrains.spek:spek-api:1.1.2'
    testCompile 'org.junit.platform:junit-platform-runner:1.0.0-M4'
    testRuntime 'org.jetbrains.spek:spek-junit-platform-engine:1.1.2'
}

sourceSets.test.java.srcDirs += 'src/test/kotlin'

src/test/kotlin/SimpleTest.kt:

import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import kotlin.test.assertEquals

class SimpleTest : Spek({
    given("simple test") {
        it("should succeed") {
            assertEquals(1, 1)
        }

        it("shouldn't succeed") {
            assertEquals(0, 1)
        }
    }
})

Gradle 测试输出:

Executing external task 'test'...
Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set. This behaviour has been deprecated and is scheduled to be removed in Gradle 5.0
:generateBuildConfig UP-TO-DATE
:compileBuildConfig UP-TO-DATE
:extractIncludeProto UP-TO-DATE
:extractProto UP-TO-DATE
:generateProto UP-TO-DATE
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestKotlin
Using kotlin incremental compilation
:extractIncludeTestProto UP-TO-DATE
:extractTestProto UP-TO-DATE
:generateTestProto NO-SOURCE
:compileTestJava NO-SOURCE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:junitPlatformTest
Test run finished after 5062 ms
[         1 containers found      ]
[         0 containers skipped    ]
[         1 containers started    ]
[         0 containers aborted    ]
[         1 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]
:test
:test SKIPPED
BUILD SUCCESSFUL in 14s
12 actionable tasks: 2 executed, 10 up-to-date
External task execution finished 'test'.

这是因为 Spek 1.1.2 和 Kotlin 1.1.4 之间存在兼容性问题https://github.com/JetBrains/spek/issues/248

这可以通过使用 Spek 1.1.4 和 junit-platform 1.0.0-RC2 来解决