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

JUnit5+Cucumber结合Suites API时出现“No tests were found”错误

JUnit5 + Cucumber Suites API 测试无法被发现的问题
  • 使用JUnit5 Suites API结合Cucumber时,无论在@Suite相关注解中如何组合标签与路径,测试均无法被识别
  • 直接执行mvn test命令也存在相同问题
  • 仅在junit-platform.properties文件中明确指定feature文件路径时,mvn test可正常执行测试,但将相同路径配置到Suite注解属性中无效(注:原本junit配置文件为空,配置类使用@Suite等注解但路径配置未生效)

补充信息

Feature文件内容

Scenario Outline: Verify user is unable to login with invalid credentials
    Given I am on Login page
    When I enter username "<username>" and password "<password>"
    Then I check login is not successful
    Examples:
      | username      | password  |
      | not_existing  | 123456789 |
      | invalid_login | test1234  |

POM文件内容

<properties>
    <log4j-core.version>2.20.0</log4j-core.version>
    <testng.version>6.14.3</testng.version>
    <owner.version>1.0.12</owner.version>
    <client-java.version>5.1.16</client-java.version>
    <playwright.version>1.35.0</playwright.version>
    <lombok.version>1.18.26</lombok.version>
    <commons-csv.version>1.10.0</commons-csv.version>
    <maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
    <maven-compiler-plugin.target>1.8</maven-compiler-plugin.target>
    <junit-jupiter-api.version>5.9.3</junit-jupiter-api.version>
    <cucumber-java.version>7.13.0</cucumber-java.version>
    <maven-surefire-plugin.version>3.0.0-M6</maven-surefire-plugin.version>
</properties>


<dependencies>
    <dependency>
        <artifactId>log4j-core</artifactId>
        <groupId>org.apache.logging.log4j</groupId>
        <version>${log4j-core.version}</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aeonbits.owner</groupId>
        <artifactId>owner</artifactId>
        <version>${owner.version}</version>
    </dependency>
    <dependency>
        <groupId>com.epam.reportportal</groupId>
        <artifactId>client-java</artifactId>
        <version>${client-java.version}</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.microsoft.playwright</groupId>
        <artifactId>playwright</artifactId>
        <version>${playwright.version}</version>
    </dependency>
    <dependency>
        <artifactId>lombok</artifactId>
        <groupId>org.projectlombok</groupId>
        <version>${lombok.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>${commons-csv.version}</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit-jupiter-api.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>${junit-jupiter-api.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber-java.version}</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>${cucumber-java.version}</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit-platform-engine</artifactId>
        <version>${cucumber-java.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite-api</artifactId>
        <version>1.10.0-M1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

问题排查与解决步骤

1. 修复JUnit版本兼容性

当前junit-platform-suite-api版本1.10.0-M1与junit-jupiter-api版本5.9.3不匹配(JUnit 5.x对应JUnit Platform 1.x,版本号前两位需一致)。修改Suite API版本并补充引擎依赖:

<!-- 替换原有Suite API依赖 -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-api</artifactId>
    <version>1.9.3</version>
    <scope>test</scope>
</dependency>
<!-- 添加Suite引擎依赖 -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-engine</artifactId>
    <version>1.9.3</version>
    <scope>test</scope>
</dependency>

2. 正确配置Suite类

Cucumber结合JUnit Suite时,需指定Cucumber引擎,并通过@ConfigurationParameter配置feature路径,示例:

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

import static io.cucumber.junit.platform.engine.Constants.FEATURES_PROPERTY_NAME;

@Suite
@IncludeEngines("cucumber")
@SelectPackages("com.yourpackage.steps") // 替换为你的step定义包路径
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "classpath:features/") // 替换为实际feature文件路径
public class CucumberTestSuite {
}

3. 移除冲突依赖

POM中重复引入了TestNG依赖(test和compile scope各一个),保留test scope的TestNG或直接移除,避免测试引擎冲突:

<!-- 移除该冲突依赖 -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>${testng.version}</version>
    <scope>compile</scope>
</dependency>

4. 配置Surefire插件

明确指定Surefire插件识别Suite类:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <includes>
                    <include>**/*TestSuite.java</include> <!-- 你的Suite类路径 -->
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

5. 验证路径配置

确保@ConfigurationParameter中的feature路径正确,如src/test/resources/features/对应classpath:features/,单个文件可写classpath:features/login.feature。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 23:43:10