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

Gradle无法识别Cucumber测试:No matching tests found问题求助

Gradle无法找到Cucumber测试的问题排查与解决

问题描述

使用Cucumber和Gherkin进行功能测试时,Gradle执行测试任务时报错:No matching tests found in any candidate test task,已尝试切换JUnit版本、咨询导师及AI工具,问题仍未解决。

相关代码文件

Feature文件(路径:src/test/resources/Login.feature)

Feature: Login
  Scenario Login with valid username & password
    Given user is on login page
    When user input email text box with "standard_user"
    And user input password on text box "secret_sauce"
    And user click Login button
    Then user will redirect to homepage

StepDef类(路径:src/test/java/com/nadif/stepdef/MyStepdefs.java)

package com.nadif.stepdef;

import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class MyStepdefs {
    @Given("user is on login page")
    public void userIsOnLoginPage() {
    }

    @When("user input email text box with {string}")
    public void userInputEmailTextBoxWith(String username) {
    }

    @And("user input password on text box {string}")
    public void userInputPasswordOnTextBox(String password) {
    }

    @And("user click Login button")
    public void userClickLoginButton() {
    }

    @Then("user will redirect to homepage")
    public void userWillRedirectToHomepage() {
    }
}

CucumberTest类(路径:src/test/java/com/nadif/CucumberTest.java)

package com.nadif;

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

@RunWith(Cucumber.class)
@CucumberOptions(
        glue = "com.nadif.stepdef",
        features = "src/test/resources",
        plugin = {"pretty"}
)
public class CucumberTest {
}

终端错误日志

No matching tests found in any candidate test task.
    Requested tests:
        Test pattern com.nadif.CucumberTest in task :test

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 3s
4 actionable tasks: 3 executed, 1 up-to-date

依赖与Gradle测试任务配置

dependencies {
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.27.0'
    testImplementation("io.github.bonigarcia:webdrivermanager:5.9.0")
    testImplementation 'io.cucumber:cucumber-java:7.20.1'
    testImplementation 'io.cucumber:cucumber-spring:7.20.0'
    testImplementation 'io.cucumber:cucumber-junit:7.20.1'
    testImplementation 'junit:junit:4.13.2'
}

test {
    useJUnit()
    include '**/CucumberTest*'
}

排查与修复方案

1. 统一Cucumber依赖版本

当前依赖中cucumber-spring版本为7.20.0,其余Cucumber依赖为7.20.1,版本不一致可能引发兼容问题,将其统一为同一版本:

testImplementation 'io.cucumber:cucumber-spring:7.20.1'

2. 修正Feature文件的Scenario语法

Gherkin语法中,Scenario关键字后需加冒号,原Feature文件中缺少冒号会导致Cucumber无法识别场景,修正后:

Feature: Login
  Scenario: Login with valid username & password
    Given user is on login page
    When user input email text box with "standard_user"
    And user input password on text box "secret_sauce"
    And user click Login button
    Then user will redirect to homepage

3. 调整Gradle测试任务配置

确保测试任务正确识别JUnit4测试,可明确指定测试类格式,或临时移除include配置验证:

test {
    useJUnit()
    include '**/CucumberTest.class'
}

4. 验证测试类文件路径

确认CucumberTest.java位于src/test/java/com/nadif/目录下,Gradle默认仅扫描src/test/java下的测试类,路径错误会导致无法识别。

5. 清理并重新构建

执行Gradle清理命令清除缓存,再重新运行测试:

gradle clean test

6. 调整CucumberOptions的features路径

将绝对路径改为classpath路径,避免路径识别问题:

@CucumberOptions(
        glue = "com.nadif.stepdef",
        features = "classpath:",
        plugin = {"pretty"}
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:42:14