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

JUnit Platform无法找到Cucumber测试的问题排查求助

问题:JUnit Platform下Cucumber场景无法运行的解决方法

原JUnit 4运行配置

@RunWith(Cucumber::class)
@CucumberOptions(
    features = ["src/features"],
    tags = "not @ignored"
)
class RunCucumberTest

迁移到JUnit Platform的尝试代码

@Suite
@IncludeEngines("cucumber")
@SelectDirectories("src/features")
@ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME, value = "pretty")
@ConfigurationParameter(key = Constants.GLUE_PROPERTY_NAME, value = "garden.ephemeral.rocket")
class RunCucumberTest

Gradle配置

tasks.withType<Test> {
    useJUnitPlatform {
        // TODO: Not needed? Doesn't seem to work with or without.
        includeEngines("cucumber")
    }
    jvmArgs("--add-modules=jdk.incubator.vector")
    // Workaround. Gradle does not include enough information to disambiguate
    // between different examples and scenarios.
    // TODO: Move to cucumber.properties?
    systemProperty("cucumber.junit-platform.naming-strategy", "long")
    // TODO: Move to junit-platform.properties?
    systemProperty("junit.jupiter.execution.parallel.enabled", "true")
}

问题现象

  • 运行Gradle的:test任务时,任务正常执行但测试报告为空。
  • 在IDEA中运行测试类时,报错:

No tests found for given includes: [garden.ephemeral.rocket.RunCucumberTest](--tests filter)

已做排查

  • 将features移至resources目录并使用@SelectClasspathResource("features"),结果无变化。
  • 克隆Java版本的Cucumber骨架项目,配置基本一致且能正常运行场景;在该项目中尝试@SelectDirectories也可正常工作。
  • 在DiscoverySelectors.selectDirectory内设置断点,程序未触发断点,怀疑JUnit未找到或运行测试套件。
  • 计划将骨架项目转为Kotlin版本测试兼容性。

解决方案

1. 统一依赖版本兼容性

确保Cucumber与JUnit Platform相关依赖版本完全匹配,建议用Cucumber BOM统一管理版本:

dependencies {
    testImplementation(platform("io.cucumber:cucumber-bom:7.14.0"))
    testImplementation("io.cucumber:cucumber-junit-platform-engine")
    testImplementation("io.cucumber:cucumber-kotlin")
    testImplementation("org.junit.platform:junit-platform-suite-api")
    testRuntimeOnly("org.junit.platform:junit-platform-suite-engine")
}

2. 修正测试类注解配置

补全标签过滤规则

原JUnit4中的tags = "not @ignored"需通过ConfigurationParameter添加:

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features") // 优先使用类路径资源,适配打包与IDE环境
@ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME, value = "pretty")
@ConfigurationParameter(key = Constants.GLUE_PROPERTY_NAME, value = "garden.ephemeral.rocket")
@ConfigurationParameter(key = Constants.FILTER_TAGS_PROPERTY_NAME, value = "not @ignored") // 新增标签过滤
class RunCucumberTest

确认注解来源

确保@Suite注解来自org.junit.platform.suite.api.Suite,避免引入错误包的同名注解。

3. 调整Gradle测试配置

移除冗余引擎配置

useJUnitPlatform块内无需手动指定includeEngines("cucumber"),测试类上的@IncludeEngines已完成配置:

tasks.withType<Test> {
    useJUnitPlatform()
    jvmArgs("--add-modules=jdk.incubator.vector")
    systemProperty("cucumber.junit-platform.naming-strategy", "long")
    systemProperty("junit.jupiter.execution.parallel.enabled", "true")
}

验证测试类扫描路径

确保RunCucumberTest位于src/test/kotlin/garden/ephemeral/rocket/下,且Gradle的sourceSets配置正确:

sourceSets {
    test {
        kotlin.srcDir("src/test/kotlin")
        resources.srcDir("src/test/resources")
    }
}

4. IDE适配调整(针对IDEA)

  • 进入Settings > Build, Execution, Deployment > Build Tools > Gradle,将Run tests using设置为Gradle,避免IDE内置运行器不兼容Cucumber引擎。
  • 执行File > Invalidate Caches...清理缓存后重启IDE,重新同步Gradle项目。

5. 验证类路径资源

若使用@SelectClasspathResource("features"),确保features目录位于src/test/resources下,可通过运行gradle testClasses后查看build/resources/test目录是否包含该文件夹来验证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:51:32