Behavior-Driven Development
Express 测试 in plain-language Given-当-然后场景使用 Cucumber and Vitest.
你将学到
- 如何 BDD extends TDD 使用 shared, readable 场景
- Given-当-然后结构 for 描述 behavior
- Using Cucumber and Vitest 到运行 BDD specs
- Bridging business 需求 and executable 测试
概念讲解
From TDD to BDD
Behavior-Driven Development (BDD) builds on TDD by expressing tests in a shared, plain-language format that product owners can read. Where TDD says "write a test," BDD says "write a scenario describing the behavior, then make it executable." The goal is the same — test-first, small steps — but the vocabulary bridges the gap between business people and developers.
Given-When-Then
A BDD scenario follows the Given-When-Then structure. Given some initial context (a user with an empty cart), When an action occurs (they add a product), Then an outcome is expected (the cart total equals the product price). This maps directly to Arrange-Act-Assert but reads as a specification. Scenarios are grouped into features, each describing one capability of the system.
Tools
Cucumber runs Gherkin .feature files, matching each Given/When/Then line to a step-definition function in your code. Vitest and Jest can express the same structure with describe and it names written in plain 中文, often with helper functions named given, when, then for readability. For teams that need non-technical stakeholders to read and edit specs, Cucumber's Gherkin is worth the overhead; for developer-only teams, plain Vitest names often suffice.
Bridging Requirements
BDD's real value is creating a shared artifact: the feature files are both the requirements and the executable tests. When a product owner changes a scenario, the test changes too, and the team immediately sees what code must shift. This keeps documentation and tests from drifting apart — the running suite is the living spec.
In Practice
If you adopt Cucumber, keep step definitions thin — put the real logic in helper modules so the steps read like a script that a non-programmer can follow. Avoid regex-heavy step matching that obscures the connection between the Gherkin line and the code; prefer simpler matchers and parameterized steps. For teams that do not need stakeholder-facing specs, plain Vitest with descriptive 描述 and 它 names written in Given-When-Then prose gives most of BDD's readability with none of the tooling overhead. The key habit, regardless of tool, is naming tests as sentences that describe behavior and expected outcomes. When a product owner can read your test names and nod, you have achieved BDD's communication goal even without a single .feature file.
示例代码
// Feature: Shopping cart
// Scenario: Adding a product increases the total
// Given a cart with no items
// When the customer adds a $10 product
// Then the cart total is $10
import { describe, it, expect } from "vitest";
import { Cart } from "./Cart";
describe("Shopping cart", () => {
it("adding a product increases the total", () => {
// Given
const cart = new Cart();
// When
cart.add({ name: "Book", price: 10 });
// Then
expect(cart.total()).toBe(10);
});
});
测试 name 是场景, and 请求体 follows Given-当-然后. 这个读取 as spec 虽然 remaining 快, executable Vitest 测试.
动手试一试
- Code Diff
Review 场景 diffs 当 business 规则更改.
- JSON Formatter
格式 structured 数据你的 Given 上下文设置.
常见错误
错误写法
编写 Gherkin steps 那个 hide 所有 logic in regex glue
正确做法
Keep 步骤定义 thin and put 真实 logic in helper 模块. Steps 应该读取如 script, 不 implementation.
错误写法
Treating BDD as documentation 练习不使用真实 assertions
正确做法
每个然后步骤必须 assert 某事 meaningful. 场景使用否 assertion 是 spec 那个 cannot 失败, 哪个是 worthless.
练习 Behavior-Driven Development
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。