bdd-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBDD Patterns
BDD 模式
Master Behavior-Driven Development patterns to write clear, business-readable specifications that drive implementation.
掌握行为驱动开发(BDD)模式,编写清晰、业务人员可读懂的规范,以此指导开发实现。
Given-When-Then Structure
Given-When-Then 结构
The fundamental BDD pattern uses three parts:
- Given: The initial context or preconditions
- When: The action or event being tested
- Then: The expected outcome or result
gherkin
Feature: User Authentication
Scenario: Successful login with valid credentials
Given a registered user with email "user@example.com"
And the user has password "secure123"
When the user submits the login form with correct credentials
Then the user should be redirected to the dashboard
And a session should be createdBDD的基础模式包含三个部分:
- Given:初始上下文或前置条件
- When:待测试的操作或事件
- Then:预期的结果或输出
gherkin
Feature: User Authentication
Scenario: Successful login with valid credentials
Given a registered user with email "user@example.com"
And the user has password "secure123"
When the user submits the login form with correct credentials
Then the user should be redirected to the dashboard
And a session should be createdFeature File Organization
Feature 文件组织
gherkin
Feature: Shopping Cart
As a customer
I want to manage items in my cart
So that I can purchase products I'm interested in
Background:
Given I am logged in as a customer
And the product catalog is available
Scenario: Add item to empty cart
Given my cart is empty
When I add "Blue T-Shirt" to my cart
Then my cart should contain 1 item
And the cart total should be $29.99
Scenario: Remove item from cart
Given my cart contains "Blue T-Shirt"
When I remove "Blue T-Shirt" from my cart
Then my cart should be emptygherkin
Feature: Shopping Cart
As a customer
I want to manage items in my cart
So that I can purchase products I'm interested in
Background:
Given I am logged in as a customer
And the product catalog is available
Scenario: Add item to empty cart
Given my cart is empty
When I add "Blue T-Shirt" to my cart
Then my cart should contain 1 item
And the cart total should be $29.99
Scenario: Remove item from cart
Given my cart contains "Blue T-Shirt"
When I remove "Blue T-Shirt" from my cart
Then my cart should be emptyScenario Outlines for Data-Driven Tests
用于数据驱动测试的 Scenario Outline
gherkin
Scenario Outline: Password validation
Given I am on the registration page
When I enter password "<password>"
Then I should see "<message>"
Examples:
| password | message |
| abc | Password too short |
| abcdefgh | Password needs a number |
| abcdefgh1 | Password accepted |
| abcdefgh1! | Password accepted |gherkin
Scenario Outline: Password validation
Given I am on the registration page
When I enter password "<password>"
Then I should see "<message>"
Examples:
| password | message |
| abc | Password too short |
| abcdefgh | Password needs a number |
| abcdefgh1 | Password accepted |
| abcdefgh1! | Password accepted |Step Definition Patterns
Step Definition 模式
ruby
undefinedruby
undefinedRuby/Cucumber example
Ruby/Cucumber example
Given('a registered user with email {string}') do |email|
@user = User.create!(email: email, password: 'password123')
end
When('the user submits the login form with correct credentials') do
visit login_path
fill_in 'Email', with: @user.email
fill_in 'Password', with: 'password123'
click_button 'Log In'
end
Then('the user should be redirected to the dashboard') do
expect(page).to have_current_path(dashboard_path)
end
undefinedGiven('a registered user with email {string}') do |email|
@user = User.create!(email: email, password: 'password123')
end
When('the user submits the login form with correct credentials') do
visit login_path
fill_in 'Email', with: @user.email
fill_in 'Password', with: 'password123'
click_button 'Log In'
end
Then('the user should be redirected to the dashboard') do
expect(page).to have_current_path(dashboard_path)
end
undefinedWhen to Use This Skill
何时使用该技能
Use bdd-patterns when you need to:
- Write acceptance tests that stakeholders can understand
- Define behavior before implementation
- Create living documentation from tests
- Bridge communication between developers and business
- Ensure features meet business requirements
在以下场景中使用bdd-patterns:
- 编写利益相关者能够理解的验收测试
- 在开发前定义功能行为
- 从测试用例生成活文档
- 搭建开发人员与业务人员之间的沟通桥梁
- 确保功能符合业务需求
Best Practices
最佳实践
- Write scenarios from the user's perspective
- Keep scenarios focused on single behaviors
- Use declarative language, not implementation details
- Reuse step definitions across scenarios
- Use Background for common setup steps
- Keep feature files organized by domain area
- 从用户视角编写场景
- 每个场景聚焦单一行为
- 使用声明式语言,避免实现细节
- 在多个场景中复用Step Definition
- 使用Background处理通用前置步骤
- 按业务领域组织Feature文件
Common Pitfalls
常见陷阱
- Writing scenarios that are too technical
- Coupling steps to specific UI implementations
- Creating overly complex scenario outlines
- Not maintaining feature files as code changes
- Mixing multiple behaviors in one scenario
- 编写过于技术化的场景
- 将步骤与特定UI实现耦合
- 创建过于复杂的Scenario Outline
- 代码变更时未维护Feature文件
- 在单个场景中混合多个行为