Java开发者Cucumber行为驱动开发指南 | AI生成和翻译

Home 2025.07

这是一份实用的端到端 Cucumber 指南——它是什么、团队为何使用它,以及如何在 Java 中高效使用它(另附 JS/Python 用户的快速说明)。


Cucumber 是什么(与不是什么)


核心组件

1) Gherkin(纯文本规范)

2) 步骤定义(代码)

3) 运行器

4) 报告


最小示例(Java, Maven)

pom.xml(关键部分)

<dependencies>
  <!-- JUnit 5 -->
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.10.2</version>
    <scope>test</scope>
  </dependency>

  <!-- Cucumber JVM + JUnit Platform -->
  <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.18.1</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit-platform-engine</artifactId>
    <version>7.18.1</version>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.2.5</version>
      <configuration>
        <!-- 如果需要,按标签运行、并行运行等 -->
      </configuration>
    </plugin>
  </plugins>
</build>

项目布局

src
 └─ test
     ├─ java
     │   └─ com/example/steps/...
     └─ resources
         └─ features/...

功能文件 (src/test/resources/features/login.feature)

Feature: 登录功能
  作为一名注册用户
  我希望能够登录
  以便我可以访问我的账户

  Background:
    Given 应用程序正在运行

  @smoke
  Scenario: 成功登录
    Given 我在登录页面
    When 我使用用户名 "alice" 和密码 "secret" 登录
    Then 我应该看到 "Welcome, alice"

  Scenario Outline: 登录失败
    Given 我在登录页面
    When 我使用用户名 "<user>" 和密码 "<pass>" 登录
    Then 我应该看到 "Invalid credentials"
    Examples:
      | user  | pass     |
      | alice | wrong    |
      | bob   | invalid  |

步骤定义(Java, Cucumber 表达式)

package com.example.steps;

import io.cucumber.java.en.*;
import static org.junit.jupiter.api.Assertions.*;

public class LoginSteps {
  private String page;
  private String message;

  @Given("the application is running")
  public void app_running() {
    // 启动测试应用 / 启动服务器 / 重置状态
  }

  @Given("I am on the login page")
  public void i_am_on_the_login_page() {
    page = "login";
  }

  @When("I sign in with username {string} and password {string}")
  public void i_sign_in(String user, String pass) {
    // 调用 UI 或 API;此处模拟:
    if ("alice".equals(user) && "secret".equals(pass)) {
      message = "Welcome, alice";
    } else {
      message = "Invalid credentials";
    }
  }

  @Then("I should see {string}")
  public void i_should_see(String expected) {
    assertEquals(expected, message);
  }
}

JUnit 5 运行器(通过引擎发现)

// 使用 JUnit Platform 时不需要显式的运行器类。
// 如果需要标签过滤,创建一个测试套件:
import org.junit.platform.suite.api.*;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = "cucumber.glue", value = "com.example.steps")
@ConfigurationParameter(key = "cucumber.plugin", value = "pretty, html:target/cucumber.html, json:target/cucumber.json")
@ExcludeTags("wip") // 示例
public class RunCucumberTest {}

运行:

mvn -q -Dtest=RunCucumberTest test

日常使用的 Gherkin 要点


Cucumber 表达式(比正则表达式更友好)

import io.cucumber.java.ParameterType;

public class ParameterTypes {
  @ParameterType("USD|CNY|EUR")
  public Currency currency(String code) { return Currency.getInstance(code); }
}

然后使用:When I pay 100 {currency}


钩子与测试生命周期


你可能需要的集成


CI/CD 与扩展


良好实践(经过实战检验)


要避免的反模式


其他语言的快速说明

JavaScript/TypeScript

Python

Ruby


何时选择 Cucumber

如果团队不维护 Gherkin,或者测试纯粹是技术性的,而单元/集成测试已经提供了清晰度,则跳过它(或谨慎使用)。


如果你告诉我你的技术栈(Spring/Quarkus?REST/UI?部署/CI 方式?),我可以为你勾勒一个可立即运行的 Cucumber 骨架,包括 Maven/Gradle 配置、推荐的插件以及适合你设置的示例流水线。


Back Donate