You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IntelliJ可运行但Gradle命令行找不到指定单元测试的问题

Gradle命令行找不到Kotlin单元测试类的解决方案

问题场景

在IntelliJ IDEA中可正常运行单元测试,但执行Gradle命令时提示No tests found for given includes:

./gradlew test --tests com.myproject.m2document.M2DocumentManagerTest

配置与代码

build.gradle配置:

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
        showStandardStreams = true // 显示测试中的System.out输出
    }
}

Kotlin单元测试代码:

package com.myproject.m2document

@TestInstance(TestInstance.Lifecycle.PER_CLASS) // 无需为BeforeAll/AfterAll方法添加static修饰符
internal class M2DocumentManagerTest {
    private val INCORRECT_LENGTH = 800
    private val FILE_INCORRECT_LENGTH_800 = "ABS_to_M2CM_incorrect_length_$INCORRECT_LENGTH.txt"
    private lateinit var m2DocumentManager: M2DocumentManager
    private lateinit var m2Parser: M2Parser

    @Test
    fun `Success read text file from test resources folder`() {
        javaClass.classLoader.getResource(FILE_INCORRECT_LENGTH_800)?.let {
            val file = File(it.file)
            val absolutePath = file.absolutePath
            assertThat(absolutePath.endsWith("/$FILE_INCORRECT_LENGTH_800"))
        }
    }
}

错误日志

> Task :app:compileKotlin NO-SOURCE
> Task :app:compileJava UP-TO-DATE
> Task :app:processResources UP-TO-DATE
> Task :app:classes UP-TO-DATE
> Task :app:compileTestKotlin UP-TO-DATE
> Task :app:compileTestJava NO-SOURCE
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses UP-TO-DATE
> Task :app:test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:test'.
>No tests found for given includes: [com.myproject.m2document.M2DocumentManagerTest](--tests filter

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 601ms
4 actionable tasks: 1 executed, 3 up-to-date

解决方法

1. 修改测试类可见性

Kotlin的internal类编译为JVM字节码后,可能因可见性限制导致Gradle测试扫描器无法识别。将测试类改为public(去掉internal修饰符):

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class M2DocumentManagerTest {
    // 原有代码不变
}

重新执行测试命令即可。

2. 使用通配符匹配测试类

若不想修改类可见性,可通过通配符匹配JVM层面的类名(适配Kotlin internal类的编译后命名规则):

./gradlew test --tests "*com.myproject.m2document.M2DocumentManagerTest*"

3. 补全测试依赖配置

确保build.gradle中引入了JUnit 5与Kotlin测试的核心依赖,否则Gradle无法正确识别Kotlin测试类:

dependencies {
    // JUnit 5核心依赖
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
    // Kotlin测试支持库
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
}

同步Gradle后重新执行测试命令。

4. 直接指定测试方法名

通过指定测试方法的全名(适配Kotlin带反引号的方法名)来精准定位测试:

./gradlew test --tests "*M2DocumentManagerTest.Success read text file from test resources folder*"

也可使用更简洁的通配符:

./gradlew test --tests "*M2DocumentManagerTest.Success*"

内容的提问来源于stack exchange,提问作者Alexei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:40:08