我想要什么?
我想使用kotlintest运行我的测试,通过单击测试类旁边的图标,我成功地从IntelliJ运行了测试。我的项目中也有JUnit 5测试。我现在开始使用Gradle Kotlin DSL,并且成功地运行了执行JUnit 5任务的Gradle任务check。
有什么问题?
kotlintest测试没有运行!它似乎不是由Gradle发现的,因为失败的测试会让Gradle任务成功。所以,
如何使用Gradle Kotlin DSL运行kotlintest测试,同时也使用JUnit 5?
我试了什么?
如何使用gradle进行测试?本质上是这个问题的旧版本,但由于使用了不同的技术: JUnit 4和Gradle,而不是Gradle Kotlin DSL,所以已经过时了。我能找到的其他问题要么是针对JUnit 4,要么是针对没有kotlintest的JUnit 5。
项目设置
我用的是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的完整项目。
发布于 2018-04-03 22:38:16
类似于@PhPirate的回答,要在gradle中使用KotlinTest 3.0.x,您需要这样做。
前两个步骤对于任何使用junit平台的项目都是常见的--包括junit5本身、KotlinTest、Spek等等。
基本的gradle配置如下所示:
apply plugin: 'org.junit.platform.gradle.plugin'
buildscript {
ext.kotlin_version = '1.2.31'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:1.1.0"
}
}
dependencies {
testCompile 'io.kotlintest:kotlintest-runner-junit5:3.0.2'
}您还可以在这里克隆分级样品回购,它有一个用KotlinTest配置的简单的gradle项目。
发布于 2019-06-21 20:34:17
对于Kotlin Gradle插件的最新版本,添加以下内容就足够了:
dependencies {
testImplementation(kotlin("test-junit5"))
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.5.2")
}
tasks {
test {
useJUnitPlatform()
}
}这是假设您想要坚持kotlin.test的接口。例如,一个小测试可能如下所示:
import kotlin.test.Test
import kotlin.test.assertEquals
class SomeTest {
@Test
fun testBar() {
assertEquals(5, 6)
}
}如果要从IDEA运行测试,则需要添加一些附加的依赖项:
dependencies {
// ...
testCompileOnly("org.junit.jupiter", "junit-jupiter-api", "5.5.2")
testCompileOnly("org.junit.jupiter", "junit-jupiter-params", "5.5.2")
}发布于 2018-04-03 20:16:42
由于kotlintest版本3,支持JUnit 5!换掉,在你的build.gradle.kts里
testCompile("io.kotlintest:kotlintest:2.0.7")通过
testCompile("io.kotlintest:kotlintest-core:3.0.2")
testCompile("io.kotlintest:kotlintest-assertions:3.0.2")
testCompile("io.kotlintest:kotlintest-runner-junit5:3.0.2")正如变化量g所阐明的那样。
然后运行Gradle任务check (在右边的Gradle工具窗口中的IntelliJ中),或者在IntelliJ中,单击gutter图标只运行特定的测试或测试集。
https://stackoverflow.com/questions/49638462
复制相似问题