[英]How to run kotlintest tests using the Gradle Kotlin DSL?
What do I want?我想要什么?
I want to run my tests using kotlintest , and I succeeded in running them from IntelliJ by clicking the icon next to the test class.我想使用kotlintest运行我的测试,我通过单击测试类旁边的图标成功地从 IntelliJ 运行它们。 I also have JUnit 5 tests in my project.
我的项目中也有JUnit 5测试。 I am now starting to use the Gradle Kotlin DSL , and I managed to run the Gradle task
check
which executed the JUnit 5 tasks.我现在开始使用Gradle Kotlin DSL ,并且我设法运行了执行 JUnit 5 任务的 Gradle 任务
check
。
What is the problem?有什么问题?
The kotlintest test is not run! kotlintest 测试没有运行! It is not discovered by Gradle it seems, as a failing test lets the Gradle task succeed.
Gradle 似乎没有发现它,因为失败的测试会让 Gradle 任务成功。 So,
所以,
How to run kotlintest tests using the Gradle Kotlin DSL while also using JUnit 5?
如何使用 Gradle Kotlin DSL 运行 kotlintest 测试,同时使用 JUnit 5?
What did I try?我尝试了什么?
How can I run kotlintest tests with gradle? 如何使用 gradle 运行 kotlintest 测试? is essentially the old version of the question but already obsolete since using different technology: JUnit 4 and Gradle instead of the Gradle Kotlin DSL.
本质上是问题的旧版本,但由于使用不同的技术而已经过时:JUnit 4 和 Gradle 而不是 Gradle Kotlin DSL。 All other question I could find are either for JUnit 4, or JUnit 5 without kotlintest .
我能找到的所有其他问题要么是针对 JUnit 4,要么是针对没有 kotlintest 的 JUnit 5 。
Project setup项目设置
I am using Gradle 4.6.我正在使用 Gradle 4.6。 My test is
我的测试是
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我的
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()
}
Since kotlintest version 3, JUnit 5 is supported!从 kotlintest 版本 3 开始,支持 JUnit 5! Just replace, in your
build.gradle.kts
,只需替换,在您的
build.gradle.kts
,
testCompile("io.kotlintest:kotlintest:2.0.7")
by由
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")
as clarified in the changelog .如更改日志中所述。
Then either run the Gradle task check
(in IntelliJ in the Gradle tool window on the right) or, in IntelliJ, click the gutter icon to run only a specific test or set of tests.然后运行 Gradle 任务
check
(在右侧 Gradle 工具窗口中的 IntelliJ 中),或者,在 IntelliJ 中,单击装订线图标以仅运行特定测试或测试集。
With more recent versions of the Kotlin Gradle plugin, adding this is sufficient:使用更新版本的 Kotlin Gradle 插件,添加它就足够了:
dependencies {
testImplementation(kotlin("test-junit5"))
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.5.2")
}
tasks {
test {
useJUnitPlatform()
}
}
This is assuming that you want to stick to the interface of kotlin.test
.这是假设您想坚持使用
kotlin.test
的界面。 For example, a small test could look like this:例如,一个小测试可能如下所示:
import kotlin.test.Test
import kotlin.test.assertEquals
class SomeTest {
@Test
fun testBar() {
assertEquals(5, 6)
}
}
If you want to run your tests from IDEA, you need to add some additional dependencies:如果你想从 IDEA 运行你的测试,你需要添加一些额外的依赖项:
dependencies {
// ...
testCompileOnly("org.junit.jupiter", "junit-jupiter-api", "5.5.2")
testCompileOnly("org.junit.jupiter", "junit-jupiter-params", "5.5.2")
}
Similar to @PhPirate's answer, to use KotlinTest 3.0.x with gradle, you need to.与@PhPirate 的回答类似,要将 KotlinTest 3.0.x 与 gradle 一起使用,您需要这样做。
The first two steps are common for any project that uses junit platform - which includes junit5 itself, KotlinTest, Spek, and so on.前两个步骤对于任何使用 junit 平台的项目都很常见——包括 junit5 本身、KotlinTest、Spek 等。
A basic gradle config looks like this:基本的 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'
}
You could also clone the gradle sample repo here which has a simple gradle project configured with KotlinTest.您还可以在此处克隆gradle 示例存储库,其中包含一个使用 KotlinTest 配置的简单 gradle 项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.