Gherkin Keywords in Cucumber



Cucumber tool uses a number of BDD (Behavior Driven Development) style keywords also known as Gherkin keywords. Let us try to understand each one of them in detail.

Cucumber Image



1. Feature

This keyword is used to provide a one liner description of the feature which is to be tested. This keyword is followed by the set of test cases to be executed for the concerned feature under test.



2. Scenario

This is a starting point of the test case run. This keyword is used to define the title of specific test case which we want to execute. This is followed by a sequence of steps of the test case in the form of Given, When, Then keywords.



3. Given

This keyword provides the initial context of the test case or the context of the user before the test actions described under the “When” keywords is performed.



4. When

This keyword describes the actions or the test step which the user performs for the given test scenario given the user is on the situation described under the “Given” keyword.



5. Then

This keywords describes the expected outcome or the results which should happen when the user performs the steps described under the “When” keywords. Usually in automation scripts, we use this for assertions.



6. And

This keyword is added as an extension to the Given, When and Then keywords when needed. Sometimes, the descriptions used with “Given”, “When” and “Then” keywords can become longer especially in case of complex test scenarios. In such cases, we use “And” keyword to keep short one liner description of test steps for clearer understanding. This also helps to break down the steps into small reusable chunks of codes (in Step Definition files).



7. But

This keyword is used when we want to add any conditions to the Given, When or Then keyword descriptions.



8. Background

One feature can have multiple test case to be tested. In such cases, we use single feature file and create multiple Scenario keywords for each of the test cases. Most often each of those test case may have similar common test steps in the beginning which acts as a precondition before the actual test case is executed. In such cases, we use Background keyword to define the steps that are common for all the test scenarios present in that Feature file.



9. Scenario Outline

This keyword is an extension of the “Scenario” keyword. It is used to perform testing of data driven test cases. This keyword is used when we want to execute a given test case scenario with different sets of test data.



10. Examples

This keyword is used under the Scenario Outline keyword to provide the test data for the test case defined under the “Scenario Outline” keyword.

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount