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

Cucumber JUnit 5 Maven环境报错:未找到测试用例求助

问题:Cucumber+JUnit 5+Maven环境下JUnit运行器提示“No tests were found”

搭建Cucumber+JUnit 5+Maven测试环境时,参照Gradle项目配置后,通过JUnit运行器执行测试始终提示No tests were found。已尝试调整路径、逐一更换依赖版本,项目结构及步骤代码看似无误,且可直接从feature文件运行测试,请求排查原因。

项目结构

src
├── main
└── test
    ├── java
    │   └── com.example
    │       └── gui
    │           ├── StarterStep.java
    │           └── TestRunner.java
    └── resources
        └── features
            └── Test1.feature

代码片段

StarterStep.java

public class StarterStep
{
    @Given("Start testing")
    public void openBrowser(){
        open("https://google.pl/");
    }
}

TestRunner.java

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = Constants.GLUE_PROPERTY_NAME, value = "com.example.gui")
@ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME,
        value = "pretty, summary, json:target/reports/cucumber-reports/cucumber.json,"
                + " io.qameta.allure.cucumber7jvm.AllureCucumber7Jvm")
@ConfigurationParameter(key = Constants.FILTER_TAGS_PROPERTY_NAME, value = "@AM")
public class TestRunner
{

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>JUnitTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <allure.version>2.27.0</allure.version>
    <cucumber.version>7.15.0</cucumber.version>
  </properties>
  <dependencies>
    <!-- JUnit 5 -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.10.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-suite-api</artifactId>
      <version>1.10.2</version>
      <scope>test</scope>
    </dependency>
    <!-- Cucumber -->
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-junit-platform-engine</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-picocontainer</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.codeborne</groupId>
      <artifactId>selenide-video-recorder</artifactId>
      <version>7.6.0</version>
      <scope>test</scope>
    </dependency>
    <!-- Allure -->
    <dependency>
      <groupId>io.qameta.allure</groupId>
      <artifactId>allure-selenide</artifactId>
      <version>${allure.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.qameta.allure</groupId>
      <artifactId>allure-cucumber7-jvm</artifactId>
      <version>${allure.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

排查解决方案

1. 补充JUnit平台运行器依赖

当前pom.xml仅引入junit-platform-suite-api,缺少测试引擎实现类,导致无法识别@Suite注解的测试类。需添加以下依赖:

<!-- JUnit Suite引擎实现 -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-engine</artifactId>
    <version>1.10.2</version>
    <scope>test</scope>
</dependency>
<!-- JUnit Jupiter测试引擎 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.10.2</version>
    <scope>test</scope>
</dependency>

2. 验证feature文件标签匹配

TestRunner中配置了@AM标签过滤,需确认Test1.feature中包含对应标签,示例:

@AM
Feature: 测试功能
  Scenario: 打开谷歌页面
    Given Start testing

若feature文件无该标签,运行器会过滤所有测试用例,导致提示无测试。

3. 配置Maven Surefire插件

默认Surefire插件可能未正确适配JUnit 5和Cucumber,需在pom.xml的build/plugins节点添加配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <includes>
            <include>**/TestRunner.java</include>
        </includes>
    </configuration>
</plugin>

4. 确认资源路径正确性

执行mvn clean test后,检查target/test-classes/features目录是否存在Test1.feature,确保Maven正确复制测试资源到类路径。

5. 统一依赖版本兼容性

确保所有JUnit相关依赖版本一致(如junit-jupiter-api、junit-platform-suite-api均为1.10.2),Cucumber 7.15.0与JUnit Platform 1.10.x版本兼容。


内容的提问来源于stack exchange,提问作者Mati.M

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 14:54:52