3. Feature Files and Gherkin Syntax

Estimated reading: 3 minutes 25 views

Feature files in Cucumber are written in Gherkin syntax, which is a plain language format designed to describe the behavior of the software in a way that is understandable to both technical and non-technical stakeholders. Gherkin’s main goal is to make the process of writing and reading tests more intuitive, focusing on clear communication of feature behavior.

Structure of a Feature File

A feature file typically begins with the keyword Feature, followed by a description of the feature being tested. Then, each feature file contains one or more scenarios, which describe specific examples of how the feature behaves under different conditions.

The structure of a feature file looks like this:

				
					Feature: <Feature Name>
  <Description of the feature>

  Scenario: <Scenario Name>
    Given <initial context>
    When <action or event>
    Then <expected outcome>

				
			

Key Elements of Gherkin Syntax

  1. Feature: Describes the functionality being tested.
  2. Scenario: A specific test case that describes a situation or behavior.
  3. Given: Describes the initial conditions or context.
  4. When: Describes the action or event that triggers a change.
  5. Then: Describes the expected outcome after the action.
  6. And/But: Used to add more conditions or describe additional actions.

Example of a Feature File

				
					Feature: User login functionality

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters a valid username and password
    And the user clicks the login button
    Then the user should be redirected to the dashboard page

  Scenario: Unsuccessful login with invalid credentials
    Given the user is on the login page
    When the user enters an invalid username or password
    But the login button is not clicked
    Then an error message should be displayed

				
			

Using Gherkin Keywords

  • Given: Sets the initial state or context for the scenario. It describes the conditions before an event happens.
  • When: Describes the action or event that happens during the scenario.
  • Then: Describes the expected outcome or result after the action is performed.
  • And/But: These keywords are used to add more conditions to the Given, When, or Then steps, helping to make the scenario more descriptive.

Example with And and But

				
					Feature: Shopping Cart

  Scenario: Adding items to the cart
    Given the user is on the product page
    When the user adds a product to the cart
    And the user adds a second product to the cart
    Then the shopping cart should contain two items

  Scenario: Removing items from the cart
    Given the user has items in the cart
    When the user removes one item from the cart
    But the cart is not empty
    Then the cart should contain one item

				
			

Conclusion

Feature files written in Gherkin syntax help describe the behavior of the system in a clear, readable format. Using Given, When, Then, And, and But in the feature files allows you to define the steps of a scenario with a natural language structure, making it easier to communicate requirements and expectations among team members, including those without technical backgrounds.

Leave a Comment

Share this Doc

3. Feature Files and Gherkin Syntax

Or copy link

CONTENTS