【发布时间】:2018-06-06 18:04:26
【问题描述】:
我已经用 Kotlin 和 Junit 5 编写了一些基本的单元测试。不幸的是,当我从 Intellij IDEA 运行它们时,Gradle 找不到这些测试。 我收到来自 Gradle 的消息“未找到给定的测试包括:[de.mhaug.phd.btcwallet.BasicTests]”和来自 Intellij 的消息“未收到测试事件”。
有趣的是,使用“./gradlew :clean :test”从命令行运行它会报告构建成功。但是,我的第一个测试显然是红色的,所以这表明 Gradle 没有执行它。
我已经尝试使用更详细的输出运行它,但没有任何帮助。这是一个最小(非)工作示例:
package de.mhaug.phd.btcwallet
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test
class BasicTests {
@Test
fun hey() {
assertEquals(3,1+1)
}
@Test
fun hey2() {
assertFalse(3==1+1)
}
}
这是我的 build.gradle:
buildscript {
ext.kotlin_version = '1.2.10'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'de.mhaug.phd'
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.2'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.2'
// testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: ''
// testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.2'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
如何让 Intellij/Gradle 执行我的测试?
【问题讨论】:
-
@JBNizet 谢谢。我的测试现在由 Gradle 执行。但是,Intellij 仍然显示消息“未找到测试事件”,尽管 est 在命令行中可见。
-
IntelliJ 可以直接执行 JUnit 测试 5。但是如果你将它配置为委托给 gradle,它就不能工作,因为它委托给标准的测试任务,而测试任务还不支持 JUnit 5。
-
我正在使用 intellij 2020.2 并且在运行课程时运行良好,但我需要 gradle refresh(甚至可能打开 build.gradle.kts 以触发索引)以通过个别测试
标签: intellij-idea gradle junit kotlin