Test Runner

With a cucumber-based framework, a feature file cannot be run on its own. There is a need to create a java class, which in turn will run this cucumber feature file.

Cucumber uses the Junit framework to run test cases. A Test Runner class is created through which the test cases are run. This class will use the Junit annotation @RunWith(), which helps JUnit to identify the test runner class. It is a starting point for Junit to start the execution of tests.

Below are some of the features of the Test Runner class:

  1. The test runner class acts as an interlink between feature files and step definition classes.
  2. With a test runner class, it is possible to run either a single feature file or multiple feature files.
  3. There are multiple types of test runners available which can be integrated with cucumber apart from JUnit runner. CLI runner, Android runner etc., are few among them.

 

Add Junit Test Runner Class

1. Create a new class in the Runner package and name it as ‘TestRunner’

2. Provide “@RunWith(Cucumber.class) annotation” before cucumber class as below:

@RunWith(Cucumber.class)

public class TestRunner {

       // This class will be empty

}

Note: This class will be empty

 

3. Import the relevant dependency for RunWith and Cucumber annotation like below:

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

 

Junit Runner with @CucumberOptions

@CucumberOptions annotation provides the path to feature files and path to step definitions.

Import @CucumberOptions dependency from import cucumber.api.CucumberOptions;

 

@RunWith(Cucumber.class)

@CucumberOptions(//cucumber options goes here)


public class TestRunner

{

}

 

@CucumberOptions features

Below are the features of the @CucumberOption. Let us look into them one by one:

Plugin:

Plugin Option is used to specify different formatting options for the output reports. The various plugin option available are:

Pretty: It prints the Gherkin source with additional colors and stack traces for errors.

HTML: This will generate an HTML report at the location mentioned.

JSON: This report contains all the information from the gherkin source in JSON Format.

JUnit: This report generates XML, which is understood by most Continuous Integration servers, who will use it to generate visual reports.

 

@RunWith(Cucumber.class)

@CucumberOptions(plugin = {"pretty","html:cucumber-html-report"})

All these options can be used together as below


@RunWith(Cucumber.class)

@CucumberOptions(plugin = {"pretty","html:cucumber-html-report",  "json:json_output/cucumber.json"})

 

Dry Run:

This option can be set as either true or false (default value is false). If it is set as true, then Cucumber will check that every Step mentioned in the Feature File has corresponding code written in the Step Definition file or not. Thus, in case any of the functions is missed in the Step Definition for any Step in Feature File, it will give us the message. It is usually set as true after writing scenarios first and implementing step definitions.

@RunWith(Cucumber.class)

@CucumberOptions(plugin = {"pretty"}, dryRun = true)

 

Monochrome:

This option can either set as true or false (default value is false). It is set as true to format the console output in a readable format. Otherwise, the output will be not in a readable format.

@RunWith(Cucumber.class)

@CucumberOptions(plugin = {"pretty"}, monochrome = true)

 

Feature:

Features Options helps Cucumber to locate the Feature file in the project folder structure. It is defined as below:

@RunWith(Cucumber.class)

@CucumberOptions(features= "Feature")

 

Glue:

Glue Options helps Cucumber to locate the Step Definition file in the project folder structure. It is defined as below:

@RunWith(Cucumber.class)

@CucumberOptions(features= "Feature"

                 ,glue= {"stepDefinition"})

 

Test Runner Code

Let us look at how the Test Runner code will look like when all the cucumber options features would be used all together:

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
features= "E:\\AutomationCode\\DemoBDD\\src\\main\\java\\com\\qa\\features"
,glue= {"com.qa.stepDefination"}
,monochrome= true
,plugin= {"pretty"}

)

public class TestRunner {

}