Tutorial 2:Introduction on how to write a First Test Script in CUCUMBER

Table of contents
Reading Time: 3 minutes

Hello Everyone, In this series of blog it’s time to look into how to write test cases in cucumber and execution of these test cases.Follow these Steps for the Same.
You can read the previous post related to installation of cucumber: here

Step 1: We will use the test package to define the location of features (Resources folder), step definitions (Java folder) and other files.

Then Create a class CucumberRunnerTest in StepDefinitions package and it will look like:

first

Step 2: We will write a feature file for eg. Addition of Numbers.

Step 3: We will create a .feature file(Written in Gherkins language) in feature package. ( for creating .feature file, select “File” option in new menu and name the file with <>.feature, which cucumber understands).

Feature file will be look like this:

second

Step 4: Next step is to add Step Definitions for this feature file. For this we have to Right click on feature file and click Run As –> Cucumber Feature

test cases in cucumber

As we run feature we get this in console output and it would suggest the methods required as part of step definition:

third

Step 5: Create a FirstStep.java file in package StepDefinitions. Copy Cucumber suggestions from console output, paste them to FirstStep.java file.

Step 6: Replace all the Throws exception with the required code, which will be executed when the test case is run. So we will write code related to our test scenario like this.

test cases in cucumber

Step 7: After completing all these steps we will execute our test cases for this we have two ways to do it:

1. Run the Runner from eclipse. by right clicking the CucumberRunnerTest file –> Run as –> JUnit Test.

This is the OUTPUT:

junit

2. We can execute this framework from terminal as well. Open command prompt. cd to project root directory. And Run following command:

  1. mvn  test (in Ubuntu) 

This is the OUTPUT:

run

3.Run the feature from eclipse. by right clicking the feature file –> Run as –> Cucumber feature.

This is the OUTPUT :

run1

Hope the blog helps you.

Written by 

Principal Architect at Knoldus Inc

1 thought on “Tutorial 2:Introduction on how to write a First Test Script in CUCUMBER2 min read

Comments are closed.