Writing scenarios with Gherkin syntax

 

Hiptest enables you to create executable specifications written in a language called Gherkin.

Gherkin

Gherkin is a plain-text language with a little extra structure. Gherkin is designed to be easy to learn by non-programmers, yet structured enough to allow concise description of examples to illustrate business rules in most real-world domains.

Here is a sample Gherkin document:


Feature: Account Holder withdraws cash

  Scenario: Account has sufficient funds
   Given the account balance is $100
     And the card is valid
     And the machine contains enough money  
    When the Account Holder requests $20
    Then the ATM should dispense $20
     And the account balance should be $80
     And the card should be returned

The main keywords in Gherkin are:

  • Feature
  • Scenario
  • Given, When, Then, And, But (Steps)
  • Background
  • Scenario outline
  • Examples

STEP 1: LET’S activate the BDD mode

In your project settings panel, activate the BDD mode.

activate the BDD mode from the project settings

This will enable you to write scenarios using the Gherkin syntax. The step editor in the scenario page will create only action words (reusable steps) and interpret the Gherkin keywords.

Step 2: create a feature

Your features are managed with folders to better organize your project.

Hiptest feature page

A feature has a description that provides the context. It is usually written this way:

As a [role]
I want [feature]
So that [benefit]

A feature contains one or many scenarios that describe its behaviors.

Step 3: write a scenario with the Given/When/Then syntax

By getting the business users, the analysts, the testers and the developers to adopt this vocabulary of “given/when/then”, lots of ambiguities fall away and conversations become more efficient.

Let’s write our first step: given the account balance is “$100”

Hiptest test with Gherkin

Hiptest interprets and highlights the keyword Given. It will also consider $100 as a parameter because of the double quotes “my parameter value“. This way you’ll be able to reuse the same step in the future with a different value.

First step of a Hiptest test with Gherkin

And after a couple of seconds we get the following scenario.

Hiptest scenario

Note that the step editor comes along with a powerful suggestion capability that enables you to reuse existing steps and design scenarios with a consistent business terminology. It’s also possible to use free text parameters in steps.

Step 4: add examples and use scenario outline

Cloning scenarios to use different values can quickly become tedious and repetitive. Scenario outlines allow you to more concisely express these examples through the use of parameters and datatable. Using our previous scenario we can create 3 examples.

Example of a Hiptest datatable

When you create a new step, you can directly use the column name between “…” to use it as a parameter:

Add a step with a Hiptest variable as a parameter value

After the step validation, you can notice that the character ‘=’ is automatically set before the column name. This indicates that the word after the ‘=’ character is a variable that will be replaced by the matching datatable column value.

BDD with Hiptest datatable

You can also write an expression as a parameter value. This expression can use the datatable column names. In this case, the expression should be surrounded by ${…}. For example, here, the parameter value is the addition of the value of the column x and the column y:

Use expression in Hiptest parameter value

After clicking on “View tests”, you get a preview of your generated tests:

Hiptest step with expression interpretation

Now that we have the scenarios completed let’s execute them.

Download the ebook

This field is required

This field is required

Please enter a valid email address

Thank you, the ebook has been sent to your mailbox

Hiptest is FREE for open source projects

We actually utilize a number of open source components, which is why Hiptest service is free for any open source project. We are giving back to the community!

Your open source project must meet the following criteria to be approved:

  • Your project is licensed under a license approved by the Open Source Initiative
  • Your project source code is available for download
  • Your project has a publicly accessible website

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close