SuperAPI AI 工具集

cucumber-fundamentals

测试与质量

查看项目

更新于 10-16 01:58

技能介绍

用途

Cucumber Fundamentals 用于掌握 Cucumber 与 Gherkin 的核心概念,服务于行为驱动开发(Behavior-Driven Development,BDD)。其目标是把软件需求写成可执行的规格说明,让开发者、测试人员与业务相关方都能读懂并共同维护。

主要能力

Gherkin 语法

使用 Given-When-Then 结构编写场景。示例:

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

关键字

  • Feature:软件特性的高层描述
  • Scenario:说明业务规则的具体示例
  • Given:上下文或前置条件
  • When:动作或事件
  • Then:预期结果
  • And/But:连接多个步骤
  • Background:所有场景共用的前置条件
  • Scenario Outline:多个示例的模板
  • Examples:Scenario Outline 的数据表

Feature 文件结构

按逻辑组织 feature 文件,示例:

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

使用数据表进行参数化测试:

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

用标签组织和过滤场景:

@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

Data Tables

向步骤传递结构化数据:

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

Doc Strings

向步骤传递多行文本:

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

使用方式

按照资料中的最佳实践编写场景:

  • 编写声明式场景——关注“做什么”,而不是“怎么做”
  • 保持场景独立——每个场景应能单独运行
  • 使用领域语言——用业务术语书写,而非技术实现细节
  • 一个场景,一个行为——一次只测试一件事
  • 避免在 Given/When/Then 中出现 UI 细节——停留在业务逻辑层面

好与坏场景对比示例:

❌ 坏(命令式、关注实现):

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"

✅ 好(声明式、关注业务):

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

关键原则

  • 活文档(Living Documentation):Feature 作为可执行的规格说明
  • 协作(Collaboration):由开发者、测试人员与业务相关方共同编写
  • 通用语言(Ubiquitous Language):一致地使用领域术语
  • 示例优于规则(Examples over Rules):用具体示例澄清需求
  • 自动化(Automation):场景即自动化测试

适用场景

适用于采用或学习行为驱动开发的团队与个人,用于编写可读、可执行的需求规格,统一业务与技术的沟通语言,并组织与过滤测试场景。

限制

资料未提供具体安装命令、运行环境、支持的编程语言、版本要求、收费或价格信息,暂未提供。