如何使用Gradle Kotlin DSL运行kotlintest测试? [英] How to run kotlintest tests using the Gradle Kotlin DSL?
问题描述
我想要什么?
我想用 kotlintest ,我成功地通过点击测试类旁边的图标从IntelliJ运行它们。
我的项目中也有 JUnit 5 测试。
我现在开始使用 Gradle Kotlin DSL ,并且我设法运行执行JUnit 5任务的Gradle任务 check
。
有什么问题?
kotlintest测试不运行!它似乎没有被Gradle发现,因为失败的测试让Gradle任务成功。所以,
如何使用Gradle Kotlin DSL运行kotlintest测试,同时使用JUnit 5?
blockquote>
我试过了什么? b
$ b本质上是旧版本这个问题已经过时了,因为使用了不同的技术:JUnit 4和Gradle,而不是Gradle Kotlin DSL。
我能找到的其他所有问题都是针对JUnit 4的,或者是没有kotlintest的JUnit 5 。
项目设置
我使用的是Gradle 4.6。
我的测试是
导入io.kotlintest.matchers.exactly
导入io.kotlintest.matchers。 shouldBe
import io.kotlintest.specs.FunSpec
$ b $ class KotlinTest:FunSpec(){
init {
testCalculate()
}
/ **这个失败的测试不会失败Gradle检查。 * /
fun testCalculate()= test(one plus one is two){
(1 + 1).toDouble()shouldBe(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()
}
依赖项{
classpath(org.junit.platform:junit-platform-gradle-plugin:1.1.0)
}
}
应用{
插件(org.junit.platform.gradle.plugin)
}
// Kotlin配置。
插件{
应用程序
kotlin(jvm)版本1.2.30
java //至少需要JUnit。
应用程序{
mainClassName =mypackage.HelloWorld
}
依赖项{
compile(kotlin( stdlib))
//防止奇怪的错误。
compile(kotlin(reflect))
// Kotlin反射。
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)
//当使用JUnit 5时,Kotlintests也不会运行。
testcompile(io.kotlintest:kotlintest:2.0.7)
}
知识库{
jcenter()
}
PS Complete项目 GitHub 。
解决方案与@PhPirate的答案类似,使用KotlinTest 3.0.x使用gradle,您需要。
- 为junit 5添加gradle插件(也称为junit平台)
- 将gradle junit插件添加到buildscript的类路径中
- 将KotlinTest junit5转换器添加到您的测试依赖项中。
前两个步骤对于任何使用junit平台的项目都很常见 - 其中包括junit5本身,KotlinTest,Spek等等。
apply plugin:'org.junit.platform.gradle.plugin'
buildscript {
ext.kotl in_version ='1.2.31'
存储库{
mavenCentral()
}
依赖项{
classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version
classpathorg.junit.platform:junit-platform-gradle-plugin:1.1.0
}
依赖关系{
testCompile'io.kotlintest:kotlintest-runner-junit5:3.0.2'
}
您也可以克隆 gradle样本库这里有一个配置了KotlinTest的简单Gradle项目。
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. I also have JUnit 5 tests in my project. 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.What is the problem?
The kotlintest test is not run! It is not discovered by Gradle it seems, as a failing test lets the Gradle task succeed. So,
How to run kotlintest tests using the Gradle Kotlin DSL while also using JUnit 5?
What did I try?
How can I run kotlintest tests with gradle? 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. All other question I could find are either for JUnit 4, or JUnit 5 without kotlintest.
Project setup
I am using 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
isimport 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 Complete project at GitHub.
解决方案Similar to @PhPirate's answer, to use KotlinTest 3.0.x with gradle, you need to.
- Add the gradle plugin for junit 5 (also called the junit platform)
- Add the gradle junit plugin to the classpath of your buildscript
- Add KotlinTest junit5 runner to your test dependencies.
The first two steps are common for any project that uses junit platform - which includes junit5 itself, KotlinTest, Spek, and so on.
A basic gradle config looks like this:
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 Kotlin DSL运行kotlintest测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!