Skip to main content

Test Runner Class in Cucumber

Once you have defined your test cases and associated step definitions, you can run your test cases using several methods. One way to achieve this is using JUnit Test Runner Class.

For this we need to create new class which will use JUnit annotation @RunWith(), which tells JUnit that current class is the test runner class. When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

@RunWith annotation tells JUnit that test should run using cucumber class which is present in io.cucumber.junit.Cucumber package.

Cucumber JUnit Runner Class

Create a new class with name Runner.java, and paste below code.

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

@RunWith(Cucumber.class)
@CucumberOptions(
		plugin = {"pretty", "html:target/cucumber"},
		features = {"features"},
		glue={"com.pb.cucumbertest.stepdefinitions"},
		monochrome = true,
		tags = {"@contactus"},
		dryRun = false,
		strict = true
		)

public class Runner {

}

Give path of your feature file in "features" and package name of your step definitions in "glue". @CucumberOptions will be explained in more detail in next tutorial.

Next step is right click on your runner class and execute it using Run As->JUnit Test.

You can also run it other eclipse IDE feature like run button on top.

Submitted by tgoswami on August 30, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.