top of page

Cucumber Tutorial

JUnit Test Runner Class

What Is JUnit Test Runner Class ?

In previous tutorial we have discussed the 'concepts of feature file and 'how it runs tests in Cucumber'.This time we will show you how to run this feature file in test runner class. Once the stepdefinition class and feature files are created, we can create a junit test runner class to run it. It uses JUnit annotation @RunWith(). This test runner class is the starting point for JUnit to start the tests execution.

How to Run Tests Using JUnit Test Runner Class ?

Create a stepdefinition class :

package stepdefinitions; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import io.cucumber.java.en.Given; public class Steps { @Given("^Navigate to google.com$") public void navigateToUrl() throws Throwable { // Write code here that turns the phrase above into concrete actions System.setProperty("webdriver.chrome.driver","..\\CucumberSeleniumProject\\src/main\\resources\\drivers\\chromedriver99.exe"); WebDriver driver = new ChromeDriver(); driver.get("https:\\www.google.com"); driver.close(); } }



Create a feature file :

Feature: Title of your feature I want to use this template for my feature file Scenario: Navigate to a url Given Navigate to google.com

Create a test runner java class as 'TestRunner' : In this test runner class import :
For @RunWith annotation -

import org.junit.runner.RunWith;

For @io.cucumber.junit.CucumberOptions annotaion -

import io.cucumber.junit.Cucumber;

Write codes in test runner class as :

package cucumberTest; import org.junit.runner.RunWith; import io.cucumber.junit.Cucumber; ​ @RunWith(Cucumber.class) @io.cucumber.junit.CucumberOptions( features = "Features" //pass the path of feature file or the folder name that contains feature file ,glue={"stepdefinitions"} //package name of step definitions classes ) ​ public class TestRunner { }

Run as 'Junit Test' . ​ Note : Here 'features' and 'glue' are cucumber options that are used to locate the feature file and stepdefinition file respectively. ​ Result would be displayed in JUnit tab. ​ Syntax :
  • To pass single feature file : features = "feature file location"

  • To pass multiple feature file : features = {"feature file 1 location","feature file 2 location","feature file 3 location"}

​ ​


Refer next page Cucumber Options
bottom of page