Given-When-Then, often referred to as Gherkin syntax, is a structured language used in Behavior-Driven Development (BDD) to define and describe the expected behavior of a software system. Gherkin syntax is designed to be human-readable and is commonly used in BDD frameworks like Cucumber and SpecFlow. It provides a clear and uniform way to express behavior scenarios.
The Gherkin Structure:
Gherkin scenarios follow a specific structure with three main sections: Given, When, and Then. Each section serves a distinct purpose in describing the expected behavior.
- Given:
- The “Given” section describes the initial context or state of the system before the behavior is tested.
- It sets up the preconditions necessary for the scenario to make sense.
- “Given” statements establish the starting point for the behavior you want to test.
- When:
- The “When” section describes an action or event that occurs, typically triggered by the user or some external factor.
- It represents the action you are testingโthe specific behavior that you want to observe or verify.
- Then:
- The “Then” section describes the expected outcome or behavior that should result from the action defined in the “When” section.
- “Then” statements articulate the specific results, changes, or behavior that you are testing for.
Example Using Gherkin Syntax:
Here’s an example of a BDD scenario written in Gherkin syntax:
Scenario: Logging into an email account
Given the user is on the email login page
When the user enters their valid username and password
Then the user should be logged into their email account
In this scenario:
- The “Given” section establishes the context by placing the user on the email login page.
- The “When” section describes the action of entering a valid username and password.
- The “Then” section specifies the expected outcome, which is that the user should be successfully logged into their email account.
Additional Keywords in Gherkin:
In addition to the primary Given-When-Then structure, Gherkin syntax also includes other keywords that can be used to enhance scenarios:
- And: The “And” keyword can be used to continue a section that has already started. For example, “Given,” “When,” or “Then” can be followed by “And” to add more context, actions, or expected outcomes.
- But: The “But” keyword is similar to “And” but is often used to introduce an unexpected outcome or exception.
- Background: The “Background” keyword is used to define a set of Given steps that are common to multiple scenarios within the same feature file. This helps avoid repetition and keeps feature files concise.
- Scenario Outline: The “Scenario Outline” keyword is used to create parameterized scenarios. It allows you to run the same scenario with different sets of data.
Gherkin syntax provides a standardized and human-readable way to express behavior scenarios in BDD. By following this structure, development teams can create clear, structured, and unambiguous descriptions of how the software should behave from the user’s perspective.