Gherkinの構文とフィーチャー
人間にも読みやすいGherkin構文を使って、フィーチャー、シナリオ、Given-When-Thenのステップを定義する方法を学びます。
「Gherkinの構文とフィーチャー」はCoddyKit上の無料Testing Mastery: JUnit, Mockito & Integration Testsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはTesting Mastery: JUnit, Mockito & Integration Tests学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Gherkin: Plain Language Tests
Gherkin is a plain-language parser used by Behavior-Driven Development (BDD) tools like Cucumber. It helps describe software behavior in a human-readable format.
This makes it easier for everyone—developers, testers, and business stakeholders—to understand and agree on what the software should do. Gherkin uses a specific set of keywords to structure these descriptions.
Defining a Feature
The Feature keyword is the starting point of any Gherkin document. It describes a high-level capability or a specific area of the application.
- Think of it as the main goal or business requirement you are testing.
- It should have a clear, concise title.
- An optional description can follow, providing more context.
- Each
.featurefile typically contains oneFeature.
Feature Example in Action
Here’s how you define a Feature. Notice the optional description lines that add clarity, explaining the 'who', 'what', and 'why'.
Feature: User Login Functionality
As a registered user
I want to log in to my account
So that I can access personalized content.Describing Scenarios
Within a Feature, individual test cases are called Scenarios. A Scenario describes a single, concrete example of behavior.
For repetitive tests with different data, Scenario Outline is used. It runs the same steps multiple times, substituting values from an Examples table (which we'll cover in future lessons).
A Simple Scenario
A Scenario typically follows the "Given-When-Then" structure. It's a specific example of how the feature behaves under certain conditions.
Feature: User Login Functionality
Scenario: Successful login with valid credentials
Given the user is on the login page
When the user enters "john@example.com" and "password123"
And clicks the "Login" button
Then the user should be redirected to the dashboard
And a welcome message "Welcome, John!" should be displayedSetting the Stage with `Given`
The Given keyword is used to describe the initial state or context of the system. It sets up the prerequisites for the scenario.
- Think of it as the "precondition" for your test.
- It should describe something that has already happened.
- Example:
Given the user is logged inorGiven a product exists with price $10.
The Action with `When`
The When keyword describes the action or event that triggers the behavior under test. This is typically an interaction by the user or an external system.
- It's the "trigger" for the behavior.
- It should describe a single, observable action.
- Example:
When the user clicks the "Add to Cart" buttonorWhen the payment is processed.
Verifying Outcome with `Then`
The Then keyword describes the expected outcome or result after the When action. It's how you verify the system's behavior.
- This is the "assertion" part of your test.
- It should describe an observable change in the system.
- Example:
Then the item should be added to the cartorThen the order status should be "Completed".
Full Gherkin Scenario
Here's a complete Scenario showing Given, When, and Then working together. Notice how And can be used to extend any step for better readability.
Feature: Shopping Cart Management
Scenario: Add item to empty cart
Given the user is on the product page for "Laptop X"
And the shopping cart is empty
When the user clicks "Add to Cart"
Then the shopping cart should contain "Laptop X"
And the cart total should be $1200.00Ordering Gherkin Steps
In Gherkin, steps follow a logical flow to describe a behavior. Arrange the keywords in the correct order for a typical scenario.
Gherkin Syntax Summary
You've learned the core elements of Gherkin syntax!
Featuredefines a high-level capability.Scenariodescribes a single example of behavior.Givensets the initial context.Whendescribes the action or event.Thenverifies the expected outcome.AndandButare used to extend steps for readability.
This human-readable language bridges the gap between technical and non-technical team members, ensuring everyone understands the desired system behavior.
よくある質問
「Gherkinの構文とフィーチャー」レッスンは無料ですか?
はい。「Gherkinの構文とフィーチャー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Testing Mastery: JUnit, Mockito & Integration Testsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。
「Gherkinの構文とフィーチャー」で何を学びますか?
人間にも読みやすいGherkin構文を使って、フィーチャー、シナリオ、Given-When-Thenのステップを定義する方法を学びます。 ブラウザで直接実行するハンズオンコードでTesting Mastery: JUnit, Mockito & Integration Testsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Testing Mastery: JUnit, Mockito & Integration Testsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのTesting Mastery: JUnit, Mockito & Integration Testsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Gherkinの構文とフィーチャー」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このTesting Mastery: JUnit, Mockito & Integration Testsレッスンでコードを書いて実行できますか?
はい。すべてのTesting Mastery: JUnit, Mockito & Integration Testsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- BDD入門
- Gherkinの構文とフィーチャー
- ステップ定義の実装
- シナリオアウトラインとデータテーブル