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

Cucumber+JUnit5测试报错:未找到指定测试用例求助

解决Cucumber + JUnit5 + Spring Boot + Gradle中"No tests found for given includes"问题

针对你遇到的问题,以下是几个关键排查方向和修复方案:

1. 修正Feature文件路径配置

你的RunCucumberTest中,@SelectClasspathResource和FEATURES_PROPERTY_NAME使用了绝对路径格式,但classpath资源路径应该相对于src/test/resources目录,不需要带上src/test/resources前缀:

@Suite
@IncludeEngines("cucumber")
// 改为相对于classpath的路径,比如feature在src/test/resources/cucumber下,直接写"cucumber"
@SelectClasspathResource("cucumber")
@ConfigurationParameters({
            @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.mycom.myproject.cucumber.steps"),
            // 同样去掉src/test/resources前缀,用classpath:明确加载路径
            @ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "classpath:cucumber"),
            @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "html:target/cucumber-report.html")
})
public class RunCucumberTest {
}

2. 修复步骤定义的注解问题

你使用了@Quand和@Alors(法语注解),但Cucumber默认不会自动识别非英文注解,有两种解决方式:

  • 方式一:改用标准英文注解
    替换为Cucumber默认的@Given、@Then:
    import io.cucumber.java.en.Given;
    import io.cucumber.java.en.Then;
    
    public class StepDefinition {
    
        @Given("I want to calculate my bill")
        public void iWantToCalculateMyBill() {
             // some code
        }
    
        @Then("I have this result")
        public void iHaveThisResult() {
             // some assertions
        }
    }
    
  • 方式二:配置Cucumber使用法语
    在RunCucumberTest的ConfigurationParameters中添加语言参数:
    @ConfigurationParameter(key = LANGUAGE_PROPERTY_NAME, value = "fr")
    
    同时导入法语版本的注解:import io.cucumber.java.fr.Quand;、import io.cucumber.java.fr.Alors;

3. 调整Gradle依赖配置

  • 将org.junit.platform:junit-platform-commons改为testImplementation,它属于测试依赖:
    testImplementation 'org.junit.platform:junit-platform-commons:1.9.0'
    
  • 移除不必要的junit-vintage-engine(若没有JUnit4测试),避免与JUnit5冲突:
    // 删除该行
    // testImplementation "org.junit.vintage:junit-vintage-engine"
    
  • 确保cucumberVersion已正确设置为7.5.0,且所有Cucumber依赖版本一致。

4. 移除步骤定义类对CucumberBootstrap的继承

CucumberBootstrap是Spring上下文配置类,步骤定义无需继承它——Cucumber会自动扫描带有@CucumberContextConfiguration的类加载上下文,继承会导致不必要的加载问题,修正后的步骤定义类:

package com.mycom.myproject.cucumber.steps;

import static org.junit.jupiter.api.Assertions.assertTrue;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;

public class StepDefinition {

    @Given("I want to calculate my bill")
    public void iWantToCalculateMyBill() {
         // some code
    }

    @Then("I have this result")
    public void iHaveThisResult() {
         // some assertions
    }
}

5. 清理IntelliJ缓存并验证配置

  • 执行File -> Invalidate Caches... -> Invalidate and Restart清除IDE缓存
  • 确认IntelliJ开启JUnit Platform支持:Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Runner中选择Run tests using: IntelliJ IDEA或Gradle
  • 检查feature文件是否放在src/test/resources/cucumber目录下,且后缀为.feature

验证测试运行

执行以下Gradle命令手动触发测试,确认是否能找到测试:

./gradlew test --tests "com.mycom.myproject.cucumber.RunCucumberTest"

内容的提问来源于stack exchange,提问作者Hải Trung Phạm

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 23:24:47