DEV Community

Cover image for Concise Gherkin - How brevity improves BDD scenarios
Ivan Poiraudeau for UpSlide

Posted on • Edited on

Concise Gherkin - How brevity improves BDD scenarios

šŸ’” New to behavior-driven development? Itā€™s a fantastic way to collaborate and produce better documentation.
Hereā€™s an introduction, with details about its dedicated language, Gherkin.

In behavior-driven development (BDD), itā€™s best practice to keep your scenarios BRIEF, an acronym for:

  • Business language: The words used in a scenario should be drawn from the business domain.
  • Real data: Examples should use concrete, real data.
  • Intention revealing: Scenarios should reveal theĀ intentĀ of what the actors in the scenario are trying to achieve, rather than describing theĀ mechanicsĀ of how they will achieve it.
  • Essential: The purpose of a scenario is to illustrate how a rule should behave. Any part of a scenario that does not directly contribute to this purpose areĀ incidentalĀ and should be removed.
  • Focused: Most scenarios should be focused on illustrating aĀ single rule.

Last year, my team at UpSlide overhauled our user management system through BDD. We noticed that focusing on brevity let us validate several principles at once: our scenarios were becoming more intention revealing, essential and focused.

Brevity reveals scenarios intention

Consider this login step:

Given I visit "/login"
When I enter "Bob" in the "user name" field
  And I enter "tester" in the "password" field
  And I press the "login" button
Then I should be greeted with a welcome message
Enter fullscreen mode Exit fullscreen mode

This scenario is filled with unimportant and brittle implementation details. By prioritizing brevity, we can simplify it to:

When Bob logs in
Then Bob should be greeted with a welcome message
Enter fullscreen mode Exit fullscreen mode

The scenario intention becomes clearer: it emphasizes the need for a welcome message upon login, without getting bogged down in implementation specifics.

Brevity keeps scenarios essential

Here is a scenario about email notifications:

Given Alice's email storage is not full
And Alice is not offline
When Alice sends an email
Then Alice should get notified that the email was sent.
Enter fullscreen mode Exit fullscreen mode

If Aliceā€™s storage is full or if sheā€™s offline, the email indeed cannot be sent so she shouldnā€™t get notified that it was.

In most email happy paths however, these conditions wonā€™t apply and the reader doesnā€™t need to know about them.
In the test implementation, we should instead ensure the system under test has enough message storage for Alice and operates as if itā€™s online. We can then streamline the scenario:

When Alice sends an email
Then Alice should get notified that the email was sent.
Enter fullscreen mode Exit fullscreen mode

Considering storage space and internet connection separately lets us explore other behaviors, each with their own scenarios:

Given Alice's email storage is full
When Alice sends an email
Then Alice should be notified that the email couldn't be sent
And that she needs to free up storage

Given Alice is offline
When Alice sends an email
Then Alice should be notified that the email couldn't be sent
And that she needs to connect to the Internet
Enter fullscreen mode Exit fullscreen mode

Brevity focuses scenarios

Follow this example from Monopoly:

Given the player went over the GO square [A]
When the player falls on a CHANCE square [B]
Then the player should earn 200ā‚©
And the player should take the top CHANCE card
Enter fullscreen mode Exit fullscreen mode

We can see that mixing [A] and [B] in other ways (not going over the GO square, not falling on the CHANCE square) could lead to a combinatorial explosion of scenarios.

Again, noting this scenario lacks brevity leads us to more independent scenarios:

When the player goes over the GO square
Then the player should earn 200ā‚©

When the player falls on a CHANCE square
Then the player should take the top CHANCE card
Enter fullscreen mode Exit fullscreen mode

More on BDD principles

In a process akin to other forms of writings, brevity helps me uncover the core ideas behind the system Iā€™m specifying.

šŸ’” If youā€™re keen on craftingĀ concise scenarios, Iā€™ve put together a step-by-step kata to assist you:

Check it here!

I didnā€™t talk much about the two other key principles, Business Language and Real Data. This video by GĆ”spĆ”r Nagy (creator of SpecFlow, a .NET BDD framework) delves deeper into them:

Top comments (0)