Input your search keywords and press Enter.

What is Cucumber Feature file and its structure?

Cucumber Feature File Inviul

We have talked a lot about Cucumber feature file from the day when we started tutorials on Behaviour Driven Development. Earlier also we have discussed the sample Cucumber feature file in the tutorial on Cucumber introduction. Today we will discuss everything about the feature file and its major components. These components are the building blocks to develop a complete cucumber feature file.

Let’s start the discussion now.

What is Cucumber Feature File?

A feature file is a test definition file which contains the specification in the form of scenarios and steps. The specifications are written with the help of readable language, primarily English, and Gherkin syntax.

Cucumber feature file shares information on what-to-do and the file name ends with .feature extension.

Since readable language along with Gherkin syntax is used to create a feature file, hence, A cucumber feature file is a business-centric.

Click on below link to know more about Gherkin syntax.

What is Gherkin in Behaviour Driven Development (BDD)?

What is the structure of the feature file in Cucumber?

A cucumber feature file consists of mainly two sections, which are listed as follows:

  • Feature Section
  • Scenario Section

Let’s discuss each one of them separately.

Cucumber Feature File InviulFeature Section

A feature is a topmost section in the Cucumber feature file. It consists of the following two components:

  • Test case title
  • Description

Below example depicts the Feature section in the feature file:

Let’s have a look at some points to remember for Feature section:

  • A feature referred to as the functionality of the story
  • The feature will start with the Feature keyword (as shown above)

Now jumping to the Description part of the Feature section.

Description

A description describes the feature in a Cucumber feature file, which is highly recommended to use as it improves the understanding of the reader. In general, a feature injection template is used to write the description of the feature file in a single or multiple lines.

Scenario Section

A scenario is a collection of statements which illustrates business rule and they are compared with the test case of the Quality Assurance. A feature file can have multiple scenarios.

Below snippet is the live example of a scenario in a feature file.

Sample Cucumber Feature File

Sample Cucumber Feature File

How to use Gherkin Syntax in Scenario of the Cucumber feature file?

We discussed that scenario is the replica of the QA test case and it is designed with the help of Gherkin syntax. Let’s discuss the uses of these Gherkin syntaxes:

Given

It defines the scene of the scenario. Given basically declares the pre-requisite or we can say it introduces us the application in a well-known state before starting the further test execution. You should avoid writing the user’s interaction with Given syntax.

When

It defines the event or action to be performed in a scenario. There should be only one When step in a scenario.

Then

It defines the outcome or result of the When step.

And

Whenever there is a repetition of Given syntax in a test case then this repetition can be overcome with replacing Given by And.

But

Whenever there is a repetition of Then syntax in a test case then this repetition can be overcome with replacing Then by But.

Below snippet shows the use of And & But syntax:

Let’ rewrite the above scenario with And & But.

How to write a scenario in Cucumber without using Gherkin syntax?

We replace Gherkin syntax by the asterisk (*) then the test steps are written without Gherkin syntax. Both the steps are valid but the steps which use Gherkin syntax is more clear and user-friendly.

Below scenario shows test steps without using Gherkin syntax.


This is all about Cucumber feature file. You can post your queries in the comment below. Don’t forget to join our Automation Facebook group for quick updates.

Join Inviul fb group

Avinash Mishra is the author of Inviul blog. He is a software engineer and blogger by choice. He loves to write blogs, and apart from blogging, he is interested in documentary film making, listening to music, traveling around the world and philanthropic activities.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *