Gradle:任务“:test”执行失败。> 未找到给定的测试包括:

Mar*_*tin 6 java junit gradle

我尝试使用 IntelliJ在 GitHub 上运行这个开源项目的单个单元测试。

Execution failed for task ':test'.
No tests found for given includes: [DNAnalyzer.MainTest.mainClassshouldExist](--tests filter)
Run Code Online (Sandbox Code Playgroud)

测试班

package DNAnalyzer;


import org.junit.jupiter.api.Test;

public class MainTest {
  @Test
  public void mainClassshouldExist() throws ClassNotFoundException {
     Class.forName("DNAnalyzer.Main");
  }
}
Run Code Online (Sandbox Code Playgroud)

构建.gradle

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.5.1/userguide/building_java_projects.html
 */

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit test framework.
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.9.1'


    // This dependency is used by the application.
    implementation "com.google.guava:guava:31.0.1-jre"

    // Picocli
    implementation "info.picocli:picocli:4.6.3"
}

application {
    // Define the main class for the application.
    mainClass = "DNAnalyzer.Main"
}

test {
    useJUnitPlatform()
}

jar {
    manifest {
        attributes 'Main-Class': 'DNAnalyzer.Main'
    }

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的

  • 更新到最新的 JUnit

    testImplementation 组:'org.junit.jupiter',名称:'junit-jupiter-api',版本:'5.9.1'

  • 将其添加到我的 build.gradle 文件中:

    测试 { useJUnitPlatform() }

目前唯一的解决方法:

当我转到“设置”->“构建工具”->“Gradle”并将测试执行设置为“运行测试:IntelliJ IDEA”时,测试将正确执行。

谢谢

M.R*_*uti 3

问题出在包含测试类的包的命名上,DNAnalyzer它并不真正遵循java命名约定,因为它以大写字母开头/包含大写字母。

当执行单个测试用例或测试类时,IntelliJ 将通过test使用特定过滤器调用任务来委托给 Gradle,如下所示 ./gradlew :test --tests "DNAnalyzer.MainTest.mainClassshouldExist"

在 Gradle 中存在这个问题:https://github.com/gradle/gradle/issues/20350:“test --tests 不适用于包含大写字母的包”。

尝试将您的测试类移动到另一个名称良好的包中,您将不会遇到此问题。