我尝试使用 IntelliJ 在 GitHub 上运行这个开源项目的单单元测试。
Execution failed for task ':test'.
No tests found for given includes: [DNAnalyzer.MainTest.mainClassshouldExist](--tests filter)
测试类
package DNAnalyzer;
import org.junit.jupiter.api.Test;
public class MainTest {
@Test
public void mainClassshouldExist() throws ClassNotFoundException {
Class.forName("DNAnalyzer.Main");
}
}
The build.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) }
}
}
我试过什么
更新到最新的 JUnit
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.9.1'
将此添加到我的build.gradle文件中:
测试 { useJUnitPlatform() }
唯一的解决方法:
当我转到设置 -> 构建工具 -> Gradle 并将测试执行设置为"运行测试:IntelliJ IDEA"时,测试会正确执行。
谢谢
问题来自包含测试类的包的命名,DNAnalyzer
,它并不真正遵循java命名约定,因为它以大写字母开头/包含大写字母。
在执行单个测试用例或测试类时,IntelliJ 将通过使用特定过滤器调用test
任务来委托给 Gradle,如下所示./gradlew :test --tests "DNAnalyzer.MainTest.mainClassshouldExist"
在 Gradle 中存在这个问题:https://github.com/gradle/gradle/issues/20350:"test --tests 不适用于包含大写字母的软件包"。
尝试将测试类移动到另一个名称良好的包中,您不会遇到此问题。