Examples to show different ways of creating cucumber feature files

Photo by Kelly Neil on UnsplashExamples to show different ways of creating cucumber feature filesArun B ChandrasekaranBlockedUnblockFollowFollowingDec 15, 2018When 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 lineIn 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 20Example 1: Feature FileExample 1: Step DefinitionApproach 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 linesIn the below stated Gherkin, 2 numbers required for addition are stated in 2 different steps.

Given first number is 10 And second number is 20Example 2: Feature FileExample 2: Step DefinitionIn 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 handlingApproach 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 inputsIn 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 FileExample 3: Feature File, same feature file modified to show addition of 3 numbersExample 3: Step Definition, Input is received as DataTable and converted to List of numbersSome 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 DataTableIn 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 FileExample 4: Step DefinitionApproach 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 ExamplesIn the below Gherkin, both input in Scenario Outline and Examples are represented as DataTables.

Example 5: Feature FileExample 5: Step Definition Inputs represented as DataTable has a mapping Java ClassIn 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.

References:CucumberComment @tag Feature: Eating too many cucumbers may not be good for you Eating too much of anything may not be good for…docs.

cucumber.

ioThe Cucumber for Java Book: Behaviour-Driven Development for Testers and Developers by Seb Rose…Use the Java version of Cucumber, the popular, open-source tool that helps teams communicate more effectively with…pragprog.

comCucumber Cookbook | PACKT BooksOver 35 hands-on recipes to efficiently master the art of behaviour-driven development using Cucumber-JVMwww.

packtpub.

comCucumber for Java – Plugins | JetBrainsWelcome to the JetBrains plugin repositoryplugins.

jetbrains.

comTidy GherkinKeep your Gherkin feature files consistent in layout, take the pain out of table formatting and cucumber step…chrome.

google.

com.

. More details

Leave a Reply