在 IntelliJ IDEA 中运行 Gradle 测试时“未找到给定包含的测试”

Arr*_*vil 17 java intellij-idea gradle junit5

由于“未找到给定包含的测试”错误,我无法在 IntelliJ IDEA 中通过 Gradle 运行测试。

我该如何解决?

GradleTests

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class GradleTests {
    @Test
    public void initTest() {
        assertTrue(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

构建.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    //testCompile group: 'junit', name: 'junit', version: '4.12'

    // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.0'
}

test {
    useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)

错误:

> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [GradleTests.initTest](filter.includeTestsMatching)
Run Code Online (Sandbox Code Playgroud)

一些注意事项:

  • JUnit 4 和 5 都重现了问题
  • IntelliJ IDEA 2019.3.3(社区版),Build #IC-193.6494.35,建于2020年2月11日
  • 测试在 src/test/java
  • 像Intelij 2019.1 更新一样改变运行程序中断 JUnit 测试没有帮助
  • 没有useJUnitPlatform()结果是一样的

Jon*_*Lee 29

我在类似的设置中遇到了这个错误,但无法用以前的答案解决它。通过这样做解决了它。

  1. 文件 > 设置 (Ctrl+Alt+S)
  2. 构建、执行、部署 > 构建工具 > gradle
  3. 使用:Intellij IDEA 运行测试

全部归功于:https : //linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/。

  • 似乎新版本的 Android Studio 不再有这个参数了。任何想法? (15认同)
  • 唯一对我有用的解决方案。我猜 gradle 插件坏了 (3认同)

Sia*_*oft 19

Gradle 不知道在哪里寻找测试。将其添加到您的应用程序 build.gradle 文件根目录(不在 android 或依赖项闭包内):

tasks.withType(Test) {
    useJUnitPlatform()

    testLogging {     // This is for logging and can be removed.
        events("passed", "skipped", "failed")
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 14

/app/build.gradle

android {
  testOptions {
    unitTests.all {
      useJUnitPlatform()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Arr*_*vil 11

Thanks to Ben Watson I've found solution. Since JUnit 5.4.0 there is aggregate artifact with both api and engine dependencies. So just adding one dependency to build.gradle resolved this issue.

testCompile ('org.junit.jupiter:junit-jupiter:5.6.0')
Run Code Online (Sandbox Code Playgroud)


ind*_*_gt 11

就我而言,我的 import 是错误的。

我需要import org.junit.jupiter.api.Test;而不是import org.junit.Test;