Cucumber Tutorial – JUnit Test Runner Class

HOME

In the previous tutorial, we showed you What is Feature File in Cucumber. In this tutorial, will show you how to run this feature file. 

Once the Feature file as well as the Step Definitions file are created, we need to create a class called Runner class to run the tests. This class will use the JUnit annotation @RunWith(), which tells JUnit what is the test runner class.

We cannot run a Feature file by its own in cucumber based framework. We need to create a Java class, which will run the Feature File. It is the starting point for JUnit to start executing the tests. TestRunner class is create under src/ test/java.

JUnit Test Runner Class

Create a new runner class file called TestRunner. 

Select and right-click on the package outline. Click on ‘New’ file. 

Give the java class a name, such as TestRunner.

Import Statements

1) ‘org.junit.runner.RunWith’ imports @RunWith annotation from the Junit class. @RunWith annotation tells JUnit that tests should run using Cucumber class present in ‘Cucumber.api.junit’ package.

2) ‘io.cucumber.junit.CucumberOptions’ imports the @CucumberOptions annotation. This annotation tells Cucumber where to look for feature files, what reporting system to use and some other things also. But as of now in the above test, we have just told it for the Feature file folder.

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

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/Feature/MyHoliday.feature", tags = { "@BookOneWayFlight"})

public classTestRunner { 
}

The first parameter, called features, provides the location of the feature file. Similarly, the second parameter, called tags, provides the tag name (scenario) which needs to run.

The feature file is place under src/test/resources, so it is add as path for the Feature file.

Run the Cucumber Test

As we know, Feature file and Step Definition file are available. Test Runner class is created to run the Cucumber Tests.

Right Click on TestRunner class and Click Run As  > JUnit Test Application

The output of Test Execution looks like as shown in below image.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s