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

运行Cucumber测试时触发PendingException,请求技术协助

解决Cucumber的PendingException异常

异常原因

io.cucumber.java.PendingException是Cucumber的默认提示异常,核心原因是:feature文件中定义的测试步骤,没有在你的StepDefinition类中找到完全匹配的实现,导致Cucumber自动生成了一段待实现的占位代码(即你看到的throw new PendingException())。

排查与修复方案

1. 严格匹配feature步骤与StepDefinition注解

你的StepDefinition中每个注解的正则表达式,必须和feature文件里的步骤文本完全对应,包括大小写、空格、标点符号:

  • 比如@Given("^Open chrome and start application$"),feature文件里的Given步骤必须是Given Open chrome and start application,不能有任何拼写或格式差异
  • 对于带参数的@When("^I enter valid \"([^\"]*)\" and \"([^\"]*)\"$"),feature里的步骤要写成When I enter valid "your-username" and "your-password",确保引号位置、参数格式和正则匹配
  • @Then("^user should be able to login successfully$")同理,feature里的Then步骤文本必须完全一致

2. 确认包路径与glue配置匹配

你的TestRunner中glue="stepDefinition",要保证:

  • StepDefinition类确实放在stepDefinition包下(Java包名区分大小写,注意统一格式)
  • 如果包名是多级结构,比如com.example.stepDefinition,glue要写完整路径

3. 检查是否有遗漏的步骤

逐行核对feature文件中的所有Given/When/Then步骤,确保每个步骤都在StepDefinition中有对应的实现方法,没有遗漏。

4. 修正代码中的其他潜在问题

你的代码还有几个需要调整的地方:

  • 把driver.get("URL")替换成实际的应用访问地址,比如driver.get("https://your-app-login-page.com")
  • 建议添加@After注解的方法来关闭浏览器,避免进程残留:
@After
public void tearDown() {
    if (driver != null) {
        driver.quit();
    }
}
  • 确保导入了所有必要的依赖包:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.cucumber.java.After;

验证

修正以上问题后,重新运行TestRunner,确认Cucumber能正确识别所有步骤实现,异常即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 05:43:13