我想要什么?
我想使用运行我的测试科特林测试 https://github.com/kotlintest/kotlintest,我通过单击测试类旁边的图标成功从 IntelliJ 运行它们。
我也有JUnit 5 https://junit.org/junit5/在我的项目中进行测试。
我现在开始使用Gradle Kotlin DSL https://github.com/gradle/kotlin-dsl,我成功运行了 Gradle 任务check
它执行 JUnit 5 任务。
问题是什么?
kotlintest 测试未运行! Gradle 似乎没有发现它,因为失败的测试会让 Gradle 任务成功。所以,
如何使用 Gradle Kotlin DSL 运行 kotlintest 测试,同时还使用 JUnit 5?
我尝试了什么?
如何使用 gradle 运行 kotlintest 测试? https://stackoverflow.com/questions/45231313/how-can-i-run-kotlintest-tests-with-gradle/48459420?noredirect=1#comment86266799_48459420本质上是问题的旧版本,但由于使用不同的技术而已经过时:JUnit 4 和 Gradle 而不是 Gradle Kotlin DSL。
我能找到的所有其他问题都是针对 JUnit 4 的,或者没有 kotlintest 的 JUnit 5 https://stackoverflow.com/a/49014534/4126843.
项目设置
我正在使用 Gradle 4.6。
我的测试是
import io.kotlintest.matchers.exactly
import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.FunSpec
class KotlinTest: FunSpec() {
init {
testCalculate()
}
/** This failing test will not fail the Gradle check. */
fun testCalculate() = test("one plus one is two") {
(1+1).toDouble() shouldBe exactly(42.0)
}
}
and my build.gradle.kts
is
import org.gradle.api.plugins.ExtensionAware
import org.junit.platform.gradle.plugin.FiltersExtension
import org.junit.platform.gradle.plugin.EnginesExtension
import org.junit.platform.gradle.plugin.JUnitPlatformExtension
group = "mypackage"
version = "0.0"
// JUnit 5
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.junit.platform:junit-platform-gradle-plugin:1.1.0")
}
}
apply {
plugin("org.junit.platform.gradle.plugin")
}
// Kotlin configuration.
plugins {
application
kotlin("jvm") version "1.2.30"
java // Required by at least JUnit.
}
application {
mainClassName = "mypackage.HelloWorld"
}
dependencies {
compile(kotlin("stdlib"))
// To "prevent strange errors".
compile(kotlin("reflect"))
// Kotlin reflection.
compile(kotlin("test"))
compile(kotlin("test-junit"))
// JUnit 5
testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.0")
testRuntime("org.junit.platform:junit-platform-console:1.1.0")
// Kotlintests are not run anyway when using JUnit 5 as well.
testCompile("io.kotlintest:kotlintest:2.0.7")
}
repositories {
jcenter()
}
PS 完成项目GitHub https://github.com/PHPirates/kotlin-template-project.