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

JUnit测试中Cucumber无法定位Feature文件的问题求助

问题:Cucumber Runner无法定位Feature文件

已按文档完成JUnit+Cucumber环境配置,但运行Cucumber Runner类时,JUnit正常执行却找不到Feature文件,尝试填写完整路径后问题依旧,无报错信息。

环境与配置信息

  • IDE:Eclipse IDE for Java Developers - 2023-06
  • 依赖版本:Cucumber 6.10.4、JUnit 4.13、junit.jupiter 5.6.2
  • 项目结构:src/test/resources/features/vendor_group.feature

Cucumber Runner代码

package br.com.winn.acceptances;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features")
public class CucumberRunner {

}

Maven依赖配置

<dependencies>
      <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version> <!-- Use a versão mais recente do Selenium 3 -->
      </dependency>
      
      <!-- Cucumber -->
      <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>6.10.4</version> <!-- Use a versão mais recente do Cucumber 6 -->
      </dependency>
      <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>6.10.4</version> <!-- Use a versão mais recente do Cucumber JUnit 6 -->
      </dependency>
      <!-- https://mvnrepository.com/artifact/junit/junit -->
      <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
      <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
      </dependency>
</dependencies>

Feature文件内容

Feature: Edit vendor group

  Scenario: Access the application
    Given  that navigate to the URL
    When fill in the field username
    And fill in the field password
    Then get access to the home app

解决方案

以下是针对性的排查和解决步骤:

1. 修正Feature路径配置

Cucumber 6.x优先推荐使用类路径相对路径,避免项目根路径的硬编码,修改CucumberOptions:

@CucumberOptions(features = "classpath:features")

或指定单个Feature文件:

@CucumberOptions(features = "classpath:features/vendor_group.feature")

2. 解决JUnit版本冲突

当前依赖同时引入JUnit 4和JUnit 5,Cucumber 6.x的cucumber-junit基于JUnit 4实现,两者共存可能导致执行逻辑混乱:

  • 若保留JUnit 4,直接移除junit-jupiter依赖:
<!-- 删除该依赖节点 -->
<dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.6.2</version>
      <scope>test</scope>
</dependency>
  • 若改用JUnit 5,替换cucumber-junit为适配JUnit 5的依赖:
<!-- 替换原cucumber-junit依赖 -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit-platform-engine</artifactId>
    <version>6.10.4</version>
    <scope>test</scope>
</dependency>

同时移除Runner类上的@RunWith(Cucumber.class)注解。

3. 验证IDE资源目录配置

在Eclipse中确保src/test/resources被正确标记为测试资源根目录:

  1. 右键项目 → Build Path → Configure Build Path
  2. 切换到Source标签页,确认src/test/resources的输出目录为target/test-classes,且状态为included

4. 检查Feature文件有效性

  • 确认文件后缀为.feature,无拼写错误
  • 检查src/test/resources/features目录结构无额外嵌套或拼写错误

5. 用Maven命令验证资源编译

先执行Maven清理编译,确保资源文件被正确复制到输出目录:

mvn clean test-compile

查看target/test-classes/features目录下是否存在vendor_group.feature,再重新运行Runner类。


内容的提问来源于stack exchange,提问作者Claudia Carolina Langer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 01:39:57