What do I want?
我想使用kotlintest运行我的测试,我通过单击测试类旁边的图标成功地从IntelliJ运行它们 . 我的项目中也有JUnit 5测试 . 我现在开始使用Gradle Kotlin DSL,并设法运行执行JUnit 5任务的Gradle任务 check
.
What is the problem?
kotlintest测试没有运行! Gradle似乎没有发现它,因为失败的测试让Gradle任务成功 . 所以,
如何使用Gradle Kotlin DSL同时使用JUnit 5运行kotlintest测试?
What did I try?
How can I run kotlintest tests with gradle?本质上是问题的旧版本,但由于使用了不同的技术已经过时了:JUnit 4和Gradle而不是Gradle Kotlin DSL . 我能找到的所有其他问题都是JUnit 4或JUnit 5 without kotlintest .
Project setup
我正在使用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)
}
}
我的 build.gradle.kts
是
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 .
2 回答
从kotlintest版本3开始,支持JUnit 5!只需替换,在
build.gradle.kts
中,通过
正如在changelog中澄清的那样 .
然后运行Gradle任务
check
(在右侧Gradle工具窗口的IntelliJ中),或者在IntelliJ中,单击装订线图标以仅运行特定测试或一组测试 .与@ PhPirate的答案类似,要使用带有gradle的KotlinTest 3.0.x,您需要 .
为junit 5添加gradle插件(也称为junit平台)
将gradle junit插件添加到buildscript的类路径中
将KotlinTest junit5 runner添加到测试依赖项中 .
对于使用junit平台的任何项目,前两个步骤都很常见 - 包括junit5本身,KotlinTest,Spek等 .
基本的gradle配置如下所示:
你也可以克隆gradle sample repo这里有一个用KotlinTest配置的简单gradle项目 .