Photo by Kelly Neil on Unsplash

Examples to show different ways of creating cucumber feature files

Arun B Chandrasekaran
4 min readDec 16, 2018

--

When I learnt Behavior Driven Development (BDD) using Cucumber it was easy, but when I sat down to write feature files and step definitions it was difficult. Creating feature files that are easy to read ended up having lots of step definition code and creating feature files to simplify development ended up having feature files that was hard to understand by business or product owner.

Learning from books like Cucumber For Java, Cucumber Cookbook and doing BDD for couple of years, I discovered different styles or ways of writing a scenario in a feature file.

Imagine we are building a calculator product and it has Addition feature. Now, let us take one scenario “Sum of numbers” and see different ways of writing a scenario for it. Assuming you have already read about Cucumber, Gherkin let me jump and show some examples of feature file with the scenario and corresponding step definitions code written in Java 8.

Examples in this article uses Java 8 and following Cucumber version,

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>

Every example will have 2 parts, first is feature file and second is corresponding step definition. Please read them slowly to understand them better.

Example 1: Inputs, first number and second number are in one line

In the below stated Gherkin, you may notice that the 2 numbers used to perform addition are stated in a single “Given” step.

Given first number is 10 and second number is 20
Example 1: Feature File
Example 1: Step Definition

Approach of having 2 inputs in one step definition should be avoided if possible. Imagine, you want to turn this scenario to support 3, 4, 5 numbers. It becomes a long line.

Example 2: Inputs, first number and second number are split into 2 lines

In the below stated Gherkin, 2 numbers required for addition are stated in 2 different steps.

Given first number is 10    
And second number is 20
Example 2: Feature File
Example 2: Step Definition

In the above step definition, there is one step definition for getting first number and another for getting second number, this can be further simplified into just one step definition as below,

Example 2: Step Definition, further simplified using optional handling

Approach of having one input per step is easy to read and recommended, considering the fact that the developer can ignore words like ‘first’, ‘second’ in steps making them as optional words in expression.

Example 3: Using Cucumber DataTable to get all inputs

In the below stated Gherkin, inputs or list of numbers are represented in a Grid called DataTable.

Given user wants to sum the following numbers:      
| 10 |
| 20 |
Example 3: Feature File
Example 3: Feature File, same feature file modified to show addition of 3 numbers
Example 3: Step Definition, Input is received as DataTable and converted to List of numbers

Some product owners like using these Grids or DataTables as they are very similar to spreadsheets look and feel. If your product owner is of this type, then using DataTable is recommended.

Example 4: Using Scenario Outline and Examples DataTable

In the below stated Gherkin, there is “Scenario Outline” instead of “Scenario” and instead of getting input from steps, the input for test is in a DataTable under Examples.

Example 4: Feature File
Example 4: Step Definition

Approach of using Scenario Outline and Examples are useful for validation use cases. Imagine you need to test 2 validations. “1. Sum function fails when first number is null”, “2. Sum function fails when second number is null”. Best way to create feature file is to use Scenario Outline with one example for each validation as stated above.

Example 5: Using DataTable in both Scenario Outline and Examples

In the below Gherkin, both input in Scenario Outline and Examples are represented as DataTables.

Example 5: Feature File
Example 5: Step Definition Inputs represented as DataTable has a mapping Java Class

In this approach, input is represented in a DataTable which is then transformed into Java Class “Numbers.java” using TypeRegistryConfigurer defined. Using DataTable with examples are preferred if there is a use case to convert DataTable to Java Class or vice versa.

Conclusion:

Feature files are executable specifications.

There are 2 pieces to the puzzle, specification and making it executable. As stated earlier, if execution (step definition code) is simplified, readability of feature file becomes hard and if readability of feature file is simplified, step definition code for execution becomes hard to maintain. The solution to art of creating and maintaining feature files is by finding a balance between readability and step definition code maintenance and this can be achieved by making developer and product owner work together to create feature files.

--

--

Arun B Chandrasekaran

Principal Software Engineer at World Fuel Services. My job is my hobby.