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

Knoldus Inc.
Knoldus - Technical Insights
3 min readMay 25, 2017

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

image

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.

fourth

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.

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com