What is Gherkin

In the previous chapter, in the specflow setup step, we have installed specflow plugin. This is an important step in the specflow environment setup as it provides support to the Gherkin language.

So, what is Gherkin?

Gherkin is a plain-text language used to describe the behavior of software in a human-readable and easy-to-understand format. It's the language that specflow understands and uses to define test scenarios.

Following are some of the key Features of Gherkin:

  • Readability: Gherkin uses a simple syntax designed for readability by anyone, not just developers or testers. It's like writing in a structured, easy-to-follow language.
  • Structured Format: Gherkin uses specific keywords to define different parts of a scenario:
    • Feature: Describes the overall feature or functionality being tested.
    • Scenario: Describes a specific test scenario. Given, When, Then, And, But: Keywords used to define the steps of a scenario.
    • Background: Describes steps that are common to all scenarios within a feature file.
    • Scenario Outline and Examples: Used for data-driven testing to test the same scenario with different inputs.
  • Expressive Scenarios: It allows for expressing different scenarios and their expected behavior in a clear, step-by-step manner.
  • Non-Technical Language: It's not code; it's written in a way that everyone involved in the software development process—business analysts, product owners, developers, testers—can understand and contribute to.

Let's see an example of a Gherkin syntax

Gherkin
Copy
Feature: Login functionality

  Scenario: Successful login
    Given the user is on the login page
    When the user enters valid credentials
    Then the user should be logged in successfully

  Scenario Outline: Invalid login attempts
    Given the user is on the login page
    When the user enters "<username>" and "<password>"
    Then the login should fail with an error message

    Examples:
      | username | password   |
      | user1    | incorrect1 |
      | user2    | wrongpass  |

In the above example, you can see how scenarios are defined using Gherkin keywords (Feature, Scenario, Given, When, Then, Scenario Outline, Examples) to describe different test cases for the login functionality.

Gherkin serves as the common language that allows everyone involved in the software development process to understand and discuss the expected behavior of the software, forming the basis for automated tests in specflow.