cucumber-fundamentals
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCucumber Fundamentals
Cucumber 基础知识
Master the core concepts of Cucumber and Gherkin for behavior-driven development.
掌握用于行为驱动开发(BDD)的Cucumber和Gherkin核心概念。
Gherkin Syntax
Gherkin 语法
Use the Given-When-Then structure for scenarios:
gherkin
Feature: User Authentication
As a user
I want to log in to the application
So that I can access my account
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter valid credentials
And I click the login button
Then I should be redirected to the dashboard
And I should see a welcome message为场景使用Given-When-Then结构:
gherkin
Feature: User Authentication
As a user
I want to log in to the application
So that I can access my account
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter valid credentials
And I click the login button
Then I should be redirected to the dashboard
And I should see a welcome messageKeywords
关键字
- Feature: High-level description of a software feature
- Scenario: Concrete example illustrating a business rule
- Given: Context or preconditions
- When: Action or event
- Then: Expected outcome
- And/But: Connect multiple steps
- Background: Common preconditions for all scenarios
- Scenario Outline: Template for multiple examples
- Examples: Data table for Scenario Outline
- Feature:软件功能的高层描述
- Scenario:说明业务规则的具体示例
- Given:上下文或前置条件
- When:执行的操作或事件
- Then:预期的结果
- And/But:连接多个步骤
- Background:所有场景的通用前置条件
- Scenario Outline:多示例的场景模板
- Examples:Scenario Outline的数据表
Feature Files
特性文件
Structure feature files logically:
gherkin
Feature: Shopping Cart Management
In order to purchase products
As a customer
I want to manage items in my shopping cart
Background:
Given I am logged in as a customer
And my shopping cart is empty
Scenario: Add product to cart
When I add a "Laptop" to my cart
Then my cart should contain 1 item
And the cart total should be "$999.99"
Scenario: Remove product from cart
Given I have a "Laptop" in my cart
When I remove the "Laptop" from my cart
Then my cart should be empty
And the cart total should be "$0.00"合理组织特性文件结构:
gherkin
Feature: Shopping Cart Management
In order to purchase products
As a customer
I want to manage items in my shopping cart
Background:
Given I am logged in as a customer
And my shopping cart is empty
Scenario: Add product to cart
When I add a "Laptop" to my cart
Then my cart should contain 1 item
And the cart total should be "$999.99"
Scenario: Remove product from cart
Given I have a "Laptop" in my cart
When I remove the "Laptop" from my cart
Then my cart should be empty
And the cart total should be "$0.00"Scenario Outlines
场景大纲
Use data tables for parameterized tests:
gherkin
Scenario Outline: Login with different user types
Given I am on the login page
When I log in as "<user_type>"
Then I should see the "<dashboard>" dashboard
And I should have "<permissions>" permissions
Examples:
| user_type | dashboard | permissions |
| admin | Admin | full_access |
| user | User | limited_access |
| guest | Public | read_only |使用数据表实现参数化测试:
gherkin
Scenario Outline: Login with different user types
Given I am on the login page
When I log in as "<user_type>"
Then I should see the "<dashboard>" dashboard
And I should have "<permissions>" permissions
Examples:
| user_type | dashboard | permissions |
| admin | Admin | full_access |
| user | User | limited_access |
| guest | Public | read_only |Tags
标签
Organize and filter scenarios with tags:
gherkin
@smoke @authentication
Scenario: User login
Given I am on the login page
When I enter valid credentials
Then I should be logged in
@wip
Scenario: Password reset
Given I am on the password reset page
# Work in progress使用标签组织和筛选场景:
gherkin
@smoke @authentication
Scenario: User login
Given I am on the login page
When I enter valid credentials
Then I should be logged in
@wip
Scenario: Password reset
Given I am on the password reset page
# Work in progressBest Practices
最佳实践
- Write declarative scenarios - Focus on what, not how
- Keep scenarios independent - Each scenario should stand alone
- Use domain language - Write in business terms, not technical implementation
- One scenario, one behavior - Test one thing at a time
- Avoid UI details in Given/When/Then - Stay at business logic level
- 编写声明式场景 - 关注做什么,而非怎么做
- 保持场景独立 - 每个场景应可独立运行
- 使用领域语言 - 用业务术语编写,而非技术实现细节
- 一个场景对应一个行为 - 每次只测试一件事
- 避免在Given/When/Then中包含UI细节 - 聚焦业务逻辑层面
Example: Good vs Bad Scenarios
示例:好场景 vs 坏场景
❌ Bad (imperative, implementation-focused):
gherkin
Scenario: Update user profile
Given I navigate to "http://example.com/profile"
When I find the element with id "firstName"
And I clear the input field
And I type "John"
And I click the button with class "save-btn"
Then I should see the text "Profile updated"✅ Good (declarative, business-focused):
gherkin
Scenario: Update user profile
Given I am on my profile page
When I update my first name to "John"
Then my profile should be saved
And I should see a success message❌ 坏场景(命令式,聚焦实现细节):
gherkin
Scenario: Update user profile
Given I navigate to "http://example.com/profile"
When I find the element with id "firstName"
And I clear the input field
And I type "John"
And I click the button with class "save-btn"
Then I should see the text "Profile updated"✅ 好场景(声明式,聚焦业务):
gherkin
Scenario: Update user profile
Given I am on my profile page
When I update my first name to "John"
Then my profile should be saved
And I should see a success messageData Tables
数据表
Pass structured data to steps:
gherkin
Scenario: Register new user
Given I am on the registration page
When I fill in the registration form:
| Field | Value |
| First Name | John |
| Last Name | Doe |
| Email | john@example.com |
| Password | SecurePass123! |
Then I should be registered successfully向步骤传递结构化数据:
gherkin
Scenario: Register new user
Given I am on the registration page
When I fill in the registration form:
| Field | Value |
| First Name | John |
| Last Name | Doe |
| Email | john@example.com |
| Password | SecurePass123! |
Then I should be registered successfullyDoc Strings
文档字符串
Pass multi-line text to steps:
gherkin
Scenario: Submit contact form
Given I am on the contact page
When I submit a message:
"""
Hello support team,
I have a question about my recent order #12345.
Please contact me at your earliest convenience.
Best regards,
John Doe
"""
Then I should see a confirmation message向步骤传递多行文本:
gherkin
Scenario: Submit contact form
Given I am on the contact page
When I submit a message:
"""
Hello support team,
I have a question about my recent order #12345.
Please contact me at your earliest convenience.
Best regards,
John Doe
"""
Then I should see a confirmation messageKey Principles
核心原则
- Living Documentation: Features serve as executable specifications
- Collaboration: Written by developers, testers, and business stakeholders
- Ubiquitous Language: Use domain terminology consistently
- Examples over Rules: Concrete examples clarify requirements
- Automation: Scenarios are automated tests
Remember: Cucumber scenarios are specifications first, tests second. They document expected behavior in a language everyone understands.
- 活文档:特性文件可作为可执行的规格说明
- 协作:由开发人员、测试人员和业务利益相关者共同编写
- 通用语言:一致使用领域术语
- 示例优先于规则:具体示例更易明确需求
- 自动化:场景可作为自动化测试执行
请记住:Cucumber场景首先是规格说明,其次才是测试。它们用所有人都能理解的语言记录预期行为。