cucumber-gherkin について
cucumber-gherkin は、Cucumber、Gherkin、Ruby のステップ定義を使う BDD テストのガイドです。実行可能な仕様を作成するチーム向けに、構文、フック、テスト実行コマンド、シナリオ構成の実践方法を説明します。
主な機能
- 01Gherkin の機能シナリオ作成
- 02Ruby ステップ定義の利用
- 03タグ・データテーブル・ドキュメント文字列
- 04フックとシナリオ状態の設定
- 05タグやシナリオを指定したテスト実行
- 06テストレポートの生成
活用シーン
- 01ドメイン用語でシステムの動作を記述
- 02実行可能な受け入れ仕様の作成
- 03タグによるテストシナリオの選択
- 04失敗したシナリオのスクリーンショット保存
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ el-feo/ ai-context --skill cucumber-gherkinインストール後、Claude Code で/cucumber-gherkinを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/best-practices.mdgherkin-syntax.mdhooks-config.mdstep-definitions.md詳細
Cucumber & Gherkin
プレーンテキストの実行可能な仕様と Ruby のステップ定義を備えた BDD テストフレームワークです。
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 Expressions: {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 > Steps > 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"
- システムがどのように動作するかではなく、何をするかを記述します
- 1 つのシナリオにつき 1 つの振る舞いとし、ステップ数は 3~5 個にします
- Background は短く保ち(≤4 行)、必要不可欠なコンテキストだけを記述します
- 関係者が理解できるドメイン用語を使います
参考資料
詳細については、以下を参照してください:
references/gherkin-syntax.md— Gherkin 言語の完全なリファレンス(キーワード、データテーブル、タグ、i18n)references/step-definitions.md— Ruby のステップ定義パターン(データテーブル、ドキュメント文字列、カスタムパラメータータイプ、World コンテキスト、構成)references/hooks-config.md— フック、cucumber.yml の設定、レポーター、Capybara との統合references/best-practices.md— アンチパターン、命名規則、コラボレーションパターン、リファクタリング
コメント
Markdown 対応