我正在为我的 Kotlin 项目使用 Spek 测试框架。我可以通过 Intellij Idea Spek 插件运行测试,但无法通过 gradle (构建或测试)运行它们。根据 SimpleTest.kt,当从 Idea 插件运行时,1 个测试成功,1 个失败,当通过 gradle 运行时,它说 1 个容器发现了 0 个测试。如何通过 gradle 设置测试的启动?
我的 gradle 和测试文件:
构建.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 解决此问题
关于java - Gradle 找不到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46095556/