mcpField
C
cucumber-gherkin

cucumber-gherkin

github.com/el-feo测试与质量12200 次安装14 周前更新
代码仓库

关于 cucumber-gherkin

cucumber-gherkin 是关于 Cucumber、Gherkin 和 Ruby 步骤定义的 BDD 测试指南,适合编写可执行规格的测试团队。它涵盖语法、钩子、运行命令和编写实践。

主要功能

  1. 01编写 Gherkin 功能场景
  2. 02使用 Ruby 步骤定义
  3. 03运用标签、数据表和文档字符串
  4. 04配置钩子与场景状态
  5. 05按标签或场景运行测试
  6. 06生成测试报告

适用场景

  1. 01用业务语言描述系统行为
  2. 02编写可执行的验收规格
  3. 03按标签筛选测试场景
  4. 04为失败场景保存截图

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/el-feo/ai-context --skill cucumber-gherkin

安装后在 Claude Code 中输入/cucumber-gherkin确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
best-practices.md
gherkin-syntax.md
hooks-config.md
step-definitions.md

详细介绍

Cucumber 与 Gherkin

BDD 测试框架,支持纯文本可执行规范和 Ruby 步骤定义。

Feature file (.feature) ──> Step Definitions (Ruby) ──> System under test

Gherkin 快速参考

Feature: Short description
  Optional multi-line description.

  Background:
    Given common setup for all scenarios

  Rule: Business rule grouping (Gherkin 6+)

    Scenario: Concrete example
      Given an initial context
      When an action occurs
      Then expected outcome
      And additional assertion
      But negative assertion

    Scenario Outline: Parameterized
      Given there are <start> items
      When I remove <remove> items
      Then I should have <remaining> items

      Examples:
        | start | remove | remaining |
        |    12 |      5 |         7 |
        |    20 |      5 |        15 |

步骤关键字: Given(设置)、When(操作)、Then(断言)、And/But(延续)、*(项目符号)

数据表:

Given the following users exist:
  | name  | email             | role  |
  | Alice | [email protected] | admin |

文档字符串:

Given a blog post with content:
  """markdown
  # My Post Title
  Content here.
  """

标签: 在 Feature/Rule/Scenario/Examples 上使用 @smoke @critical。表达式:@smoke and not @slow、(@smoke or @critical) and not @wip

Ruby 步骤定义

# Cucumber Expressions (preferred)
Given('I have {int} cucumbers in my belly') do |count|
  @belly = Belly.new
  @belly.eat(count)
end

When('I wait {int} hour(s)') do |hours|
  @belly.wait(hours)
end

Then('my belly should growl') do
  expect(@belly.growling?).to be true
end

# Data table
Given('the following users exist:') do |table|
  table.hashes.each { |row| User.create!(row) }
end

# Doc string
Given('a JSON payload:') do |json|
  @payload = JSON.parse(json)
end

Cucumber 表达式: {int}、{float}、{word}、{string}、{}(匿名)。可选形式:cucumber(s)。备选形式:color/colour。

状态共享: 使用实例变量(@user)或 World 模块:

module MyWorld
  def current_user
    @current_user ||= create(:user)
  end
end
World(MyWorld)

钩子

Before do |scenario|
  @browser = Browser.new
end

After do |scenario|
  save_screenshot("failure.png") if scenario.failed?
end

Before('@database') do
  DatabaseCleaner.start
end

After('@database') do
  DatabaseCleaner.clean
end

BeforeAll do
  # once before any scenario
end

AfterAll do
  # once after all scenarios
end

顺序: 每个场景中,BeforeAll >(Before > Background > 步骤 > After)> AfterAll

运行

bundle exec cucumber
cucumber --tags "@smoke and not @wip"
cucumber features/login.feature:10
cucumber --dry-run
cucumber --format html --out report.html
bundle exec parallel_cucumber features/

最佳实践

声明式优先于命令式:

# Good                          # Avoid
When "Bob" logs in              When I visit "/login"
Then he sees his dashboard      And I enter "bob" in "username"
                                And I click "Login"
  • 描述系统做什么,而非怎么做
  • 每个场景只描述一种行为,包含 3-5 个步骤
  • 保持 Background 简短(≤4 行),仅包含必要上下文
  • 使用利益相关者能够理解的领域语言

参考资料

详细内容请参阅:

  • references/gherkin-syntax.md — 完整的 Gherkin 语言参考(关键字、数据表、标签、国际化)
  • references/step-definitions.md — Ruby 步骤定义模式(数据表、文档字符串、自定义参数类型、World 上下文、组织方式)
  • references/hooks-config.md — 钩子、cucumber.yml 配置、报告器、Capybara 集成
  • references/best-practices.md — 反模式、命名约定、协作模式、重构

评论

支持 Markdown