简体   繁体   English

使用 Intellij 运行测试时“未收到测试事件”

[英]"Test events were not received" when run tests using Intellij

I have a Kotlin project and when I run my JUnit tests, I can't see tests execution result in IntelliJ and get this message instead:我有一个 Kotlin 项目,当我运行 JUnit 测试时,我在 IntelliJ 中看不到测试执行结果,而是收到此消息:

test events were not received未收到测试事件

I'm using this setup:我正在使用这个设置:

macOS Mojave

Intellij CE 2019.2

JDK 11.0.3

Kotlin 1.3.50

Gradle 5.2.1

JUnit 4.12

Can you help me?你能帮助我吗?

For me, the Same issue was occurring, below changes worked for me.对我来说,发生了同样的问题,以下更改对我有用。 IntelliJ Version I am using: 2019.2.2我正在使用的 IntelliJ 版本:2019.2.2
In IntelliJ IDE, Go to在 IntelliJ IDE 中,转到

File -> Settings ->Build,Execution, Deployment -> Build Tools -> Gradle文件 -> 设置 -> 构建、执行、部署 -> 构建工具 -> Gradle

here in the Run test using: dropdown selected option was: Gradle(default) changed it to IntelliJ IDEA这里在Run test using: dropdown selected 选项是: Gradle(default)将其更改为IntelliJ IDEA

Update to 2019.2.2 or later, which contains the fix for the related issue .更新到2019.2.2或更高版本,其中包含相关问题的修复。

A workaround is to run the tests using IntelliJ IDEA instead of Gradle by changing the delegation option .一种解决方法是通过更改委托选项来使用 IntelliJ IDEA 而不是 Gradle 运行测试。

When trying to work out this problem for myself, I discovered JUnit 4 worked, but JUnit 5 reports "Tests were not received."在尝试为自己解决这个问题时,我发现 JUnit 4 可以工作,但 JUnit 5 报告“未收到测试”。 Per petrikainulainen.net I found my solution.petrikainulainen.net我找到了我的解决方案。

Even though Gradle 4.6 (and obviously all newer versions) has a native support for JUnit 5, this support is not enabled by default.尽管 Gradle 4.6(显然是所有较新的版本)都原生支持 JUnit 5,但默认情况下不启用此支持。 If we want to enable it, we have to ensure that the test task uses JUnit 5 instead of JUnit 4.如果我们想启用它,我们必须确保测试任务使用 JUnit 5 而不是 JUnit 4。

When I added the following code to Gradle, JUnit 5 worked.当我将以下代码添加到 Gradle 时,JUnit 5 起作用了。

test {
    useJUnitPlatform()
}

For anyone that is still facing this -对于仍然面临这个问题的任何人-
check if you have any compiler errors in the Build tab.检查Build选项卡中是否有任何编译器错误。

In my case I had an invalid Gradle JVM configuration (configured jdk was removed from filesystem).在我的情况下,我有一个无效的 Gradle JVM 配置(配置的 jdk 已从文件系统中删除)。

To Fix it had to change Gradle JVM on File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM.要修复它,必须在 File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM 上更改 Gradle JVM。

It was red due to an invalid JDK path.由于 JDK 路径无效,它是红色的。

IntelliJ version: 2019.2.3 IntelliJ 版本:2019.2.3

我经常不得不删除 build 和 out 文件夹来解决这个问题..

For a Spring boot, gradle project using IntelliJ, my issue was that Spring Boot currently brings JUnit 5 with it.对于使用 IntelliJ 的 Spring Boot、gradle 项目,我的问题是 Spring Boot 目前带来了 JUnit 5。 I had also manually installed JUnit 5 as well.我也手动安装了 JUnit 5。

After deleting compile 'org.junit.jupiter:junit-jupiter-api:5.6.3' in build.gradle my tests began to work again.build.gradle中删除compile 'org.junit.jupiter:junit-jupiter-api:5.6.3'后,我的测试又开始工作了。

make sure you are using the correct @Test annotation which is from org.junit.Test;确保您使用的是来自 org.junit.Test 的正确 @Test 注释; Sometimes while auto importing it can get the annotation from some other library.有时在自动导入时,它可以从其他库中获取注释。 for ex - org.junit.jupiter.api.Test;对于前 - org.junit.jupiter.api.Test;

I was using facing the same issue even using IntelliJ IDEA 2019.2.4 (top answer said this was fixed on 2019.2.2).即使使用 IntelliJ IDEA 2019.2.4,我也面临同样的问题(最佳答案说这已在 2019.2.2 上修复)。 Turns out I had to Invalidate Caches / Restart... before trying.原来我必须在尝试之前使Invalidate Caches / Restart...

For those who still might have this problem using IntelliJ & Kotlin:对于那些使用 IntelliJ 和 Kotlin 仍然可能遇到此问题的人:

Add the following lines to the build.grade.kts.将以下行添加到 build.grade.kts。 I hope it helps some of you.我希望它对你们中的一些人有所帮助。

dependencies {
    testImplementation(kotlin("test-junit5"))
    testImplementation(platform("org.junit:junit-bom:5.7.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")}
tasks.test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}

PS: without adding the "task." PS:不加“任务”。 before test{...} I was getting the following error!在 test{...} 之前我收到以下错误!

the function invoke() is not found未找到函数调用()

I ran into this issue and it turned out that I had written JUnit 4 tests in a module that was configured for JUnit 5. The solution was to use JUnit 5 imports in my test class:我遇到了这个问题,结果发现我在为 JUnit 5 配置的模块中编写了 JUnit 4 测试。解决方案是在我的测试类中使用 JUnit 5 导入:

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

For Junit 5 tests add this in your app-level build.gradle file under android{} block.对于 Junit 5 测试,将此添加到您的应用级build.gradle文件中的android{}块下。

testOptions {
    unitTests {
        all {
            useJUnitPlatform()
        }
    }
}

The problem occurred as I used both following plugins at the same time当我同时使用以下两个插件时出现问题

plugins
{
  id 'org.springframework.boot' version '2.2.6.RELEASE'
  id 'io.spring.dependency-management' version '1.0.7.RELEASE'
  ...
}

after deletion of the second one, the tests were found and executed.删除第二个后,发现并执行了测试。

Answer回答

I know of some issues that occur when running Gradle together with the newest JDK versions (13 & 14).我知道将 Gradle 与最新的 JDK 版本(13 和 14)一起运行时会出现一些问题。 If you selected either version 13 or 14 of the JVM as the Gradle JVM try using either 8 or 11.如果您选择 JVM 版本 13 或 14 作为 Gradle JVM,请尝试使用 8 或 11。

Switch the Gradle JVM to version 11:将 Gradle JVM 切换到版本 11:

File -> Settings -> Build Execution, etc -> Built Tools -> Gradle -> Gradle JVM

Explanation解释

I believe that by default Intellij will use the project SDK:我相信默认情况下 Intellij 将使用项目 SDK:

Project strcuture -> Project -> Project SDK

I've had this on several occasions now and picking version 11 of the JVM fixed things for me.我现在已经有好几次了,并且选择了 JVM 版本 11 为我修复了问题。

Worth noting is that for most of my project I'm using:值得注意的是,对于我正在使用的大部分项目:

  • Lombok龙目岛
  • Jackson杰克逊
  • Lombok plugin (Require annotation processing to be enabled) Lombok 插件(需要启用注释处理)

I don't remember where I read it but I believe there were some issues with the lombok plugin + gradle + JVM version.我不记得我在哪里读过它,但我相信 lombok 插件 + gradle + JVM 版本存在一些问题。 I might revisit this answer if I can remember where I found the post about it.如果我记得我在哪里找到关于它的帖子,我可能会重新审视这个答案。

your most likey not tellinig the system how to run the tests.您最有可能不告诉系统如何运行测试。

on your class header define a test runner like this for example:在您的类头上定义一个像这样的测试运行器,例如:

@RunWith(MockitoJUnitRunner::class)
class MYTest {//...}

or can define junitX runner , etc或者可以定义 junitX runner 等

Try add尝试添加

testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")

if your tests use the older org.junit.Test instead of org.junit.jupiter.api.Test如果您的测试使用旧的org.junit.Test而不是org.junit.jupiter.api.Test

This is the solution worked for me:这是对我有用的解决方案:

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

Switching Gradle JVM to: JAVA_HOME version 11.0.10 worked. 

Before it was Project SDK.之前是 Project SDK。 在此处输入图像描述

For me 'resetting' the Gradle setting worked.对我来说,“重置” Gradle 设置有效。

在此处输入图像描述

If you are using Kotlin DSL as your gradle script then adding the following did the trick for me:如果您使用 Kotlin DSL 作为您的 gradle 脚本,那么添加以下内容对我有用:

tasks.withType<Test> {
    useJUnitPlatform() // Make all tests use JUnit 5
}

Explanation: Above idiomatic Kotlin DSL makes all tasks with type Test to use JUnit 5 platform which is required for test to work.说明:上述惯用的 Kotlin DSL 使所有类型为 Test 的任务都使用测试工作所需的 JUnit 5 平台。

I was also struggling, and no answer online I found worked for me, and I eventually realized it was due to a System.exit(0) call.我也在挣扎,我发现网上没有答案对我有用,我最终意识到这是由于 System.exit(0) 调用。 As a result, all tests would silently stop after calling whatever code with the exit call and show that test as "ignored" in IntelliJ.因此,在使用 exit 调用调用任何代码后,所有测试都会静默停止,并在 IntelliJ 中将该测试显示为“已忽略”。

In my case Gradle offline mode was active on Android studio.在我的情况下, Gradle 离线模式在 Android 工作室上处于活动状态。 Just toggled the offline to online and it started working.只需将离线切换到在线,它就开始工作了。

This command helped me to fix this issue:这个命令帮助我解决了这个问题:

gradle clean

Clearing the gradle cache worked for me;清除gradle缓存对我有用;

./gradlew clean test --no-build-cache

If it doesn't work, you can try this one如果它不起作用,你可以试试这个

./gradlew clean test --no-build-cache --no-configuration-cache --rerun-tasks

This solution is mentioned in the JetBrains issue tracker. JetBrains 问题跟踪器中提到了此解决方案。 I just wanted to it put here as a shortcut.我只是想把它放在这里作为快捷方式。

For me, this is helpful to click "Help -> Check for Updates..."对我来说,单击“帮助-> 检查更新...”很有帮助

Help -> Check for Updates...帮助 -> 检查更新...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM