- QATestLab Blog >
- Automated Testing >
- What Is the Gherkin Language?
Note: this article was updated in August 2019.
Gherkin is a line-oriented language such as Python. The language may serve as project documentation, e.i., describes business logic of the system, and as a basis for writing automated tests using the Cucumber tool.
Gherkin has the structure of the natural language and describes the object in a form of narration. Its main advantage is that the language is comprehensible to humans. Gherkin has 10 keywords: given, when, then, and, but, scenario, feature, background, outline, examples.
Gherkin keywords
- Feature – a short but full description of the functional.
- Scenario – a particular business situation or behavior (it can be reviewed as the steps to reproduce).
- Scenario Outline helps to reduce the code and specify several parameters in one scenario.
- Background can be an attribute of Scenario (it should be brief and comprehensive).
Every feature includes several scenarios, while every scenario consists of the list of specified steps. At the beginning of every step, there is one keyword – Given, When, Then, But or And. For example, the input of invalid email by a user should be prevented. Within Gherkin syntax, the same requirement looks like this:
If a user inputs an email that does not include @, when he/she tries to submit the form, the error message with advice to enter valid email should be displayed.
As a result, you have a concrete requirement – acceptance criterion – that is easier to comprehend and that prevents the issue occurrence. Such criteria can be prepared by a Product Manager before the team starts working on a feature. As they are written in a human-understandable way, designers and other members of the project team can easily interpret a user flow.
As it’s been mentioned before, Gherkin is also used as a skeleton for automated tests. It can be implied for Agile projects. Let’s review such cases in more details.
Automation testing with Gherkin in Agile teams
Rather often Agile teams apply BDD (Behavior-Driven Development) approach where software development starts with the preparation of user stories and acceptance criteria. And we’ve already known that these criteria can be written using Gherkin.
For automation of acceptance criteria testing, Cucumber is used. It is a tool that runs automated acceptance tests prepared in BDD format. Cucumber also enables the translation of plain-text descriptions written in Gherkin into automated tests. For example:
To experience Gherkin advantages, you should keep in mind some principles while writing scripts.
Recommendations on preparing Gherkin scripts
- Execution of every scenario should be separate
- Scenario should be connected with requirements
- Steps should be easy to comprehend
- Each feature should support a separate execution
- Common scenarios are better to combine
Gherkin is a simple language that can be understood by non-programmers. It serves as a format for Cucumber specification and as a functional specification. However, to prepare Gherkin scripts, you should be deeply engaged in business logic.
No Comments Yet!
You can be the one to start a conversation.