"No tests found for given includes" when 运行 Gradle 在 IntelliJ IDEA 中测试

"No tests found for given includes" when running Gradle tests in IntelliJ IDEA

由于 "No tests found for given includes" 错误,我无法在 IntelliJ IDEA 中通过 Gradle 进行 运行 测试。

我该如何解决?

Gradle测试

import org.junit.jupiter.api.Test;

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

public class GradleTests {
    @Test
    public void initTest() {
        assertTrue(true);
    }
}

build.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()
}

错误:

> 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)

一些注意事项:

感谢 Ben Watson 我找到了解决方案。自 JUnit 5.4.0 以来,存在具有 api 和引擎依赖项的聚合工件。因此,只需向 build.gradle 添加一个依赖项即可解决此问题。

testCompile ('org.junit.jupiter:junit-jupiter:5.6.0')

Gradle 在选择其选择器时区分大小写。 See here 您可能需要将 "GradleTests" 更改为 "gradleTests"

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

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

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

使用 IntelliJ 编辑 Kotlin 项目时出现此行为的另一个原因是用于测试的文件夹,Java 类 需要位于 java 和 Kotlin 的子文件夹中类 在 kotlin 的子文件夹中。

这是我创建的一个小型项目示例,它演示了文件夹结构 https://github.com/znsio/AutomatedTestingWithKotlin/tree/main/src/test

我找到了下面引用的解释以及 link 来源:

"我的问题出在通向 kotlin 和 java 测试的单一路径中。所以 kotlin 测试在 root/src/test/kotlin/package 中,它们 运行 可以 [=11] =] 和 java 测试必须在 root/src/test/java/package 中。否则 compileTestKotlincompileTestJava 都不会找到要编译的 java 测试。" https://discuss.kotlinlang.org/t/not-able-to-run-unit-tests-on-kotlin-multiplatform-project/15779/7

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

tasks.withType(Test) {
    useJUnitPlatform()

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

/app/build.gradle

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

我遇到错误:未找到给定的测试包括:... (filter.includeTestsMatching).

在检查了 JUnit 依赖后,我发现了 junit-4.13.1 版本。

已修复,将此依赖项替换为 org.junit.jupiter.api-5.7.0.

确保测试方法被授予 public 访问权限。