Do the words “given when then” sound familiar to you? These are indeed the three keywords of the Gherkin language used to create acceptance criteria. In this article, we’ll explore how it works.
Definition of Gherkin
Gherkin, also known as “given when then,” is a natural language for explaining expected behaviors in a context; this language aims to be simple and understandable to everyone who reads it.
Gherkin has the advantage of existing in dozens of different languages. While in English, it’s written as “given when then,” in French, it translates to “étant donné… quand… alors.”
Software development practices like BDD (Behavior-Driven Development) or ATDD (Acceptance Test-Driven Development) use this language for describing expected behaviors.
Writing a Gherkin Test
Now, let’s see how to write a Gherkin test in practice.
Given that I am on the shopping cart page
When I decide to remove the product with ID "255" from the cart
Then the product with ID "255" is no longer displayed
This test is very simplistic, but the goal is primarily to show you how the structure works. The numbers in parentheses generally indicate an element that can be modified. In developer terms, this transforms into a variable.
Creating Scenarios and Adding Conditions
In Gherkin, it’s also possible to create multiple possible tests for the same scenario, explaining different result cases based on certain criteria.
Scenario: Displaying the product price
Given that I am browsing the e-commerce site
When I display a product page
And this product has a promotion of “0” %
Then the displayed price of the product remains unchangedGiven that I am browsing the e-commerce site
When I display a product page
And this product has a promotion of “20” %
Then the displayed price of the product is “-20%”
And a “promotion -20%” notice indicates the promotion
As you can see, the scenario describes two different behaviors, and it’s possible to use the “And” keyword to structure multiple possible conditions.
Introducing Data Sets
Gherkin also allows the introduction of data sets, so you’re not focusing on a single case. This is represented by writing a table with the “|” character; those familiar with SQL might see a similarity.
Let’s revisit our previous case, where we could couple the two behaviors and even add a third:
Scenario: Displaying the product price
Given that I am viewing a product page
| product id | price |
| 255 | 300 |
| 234 | 1000 |
| 567 | 100 |When the applied promotion is
| promotion |
| 0% |
| 20% |
| 50% |Then the displayed price of the product is
| displayed price |
| 300 |
| 800 |
| 50 |
Anyone reading these scenarios should have no trouble understanding what behaviors we want to test.
Conclusion – Gherkin: Given When Then
You now have the basics of Gherkin. Many software tools like Behat or Cucumber can transform this language into application code to automate tests.
If you’d like us to delve further into this topic, please don’t hesitate to let us know in the comments.
Be the first to comment