</>

Technology

Cucumber

Difficulty

Intermediate

Interview Question

What is the meaning of the TestRunner class in Cucumber?

Answer

TestRunner Class in Cucumber

Definition

The TestRunner class is the starting point of execution for Cucumber tests. It is typically an empty class (no code in the body) that uses annotations to configure Cucumber behavior.

Why It''s Empty

The TestRunner class itself contains no code — all configuration is done through the @CucumberOptions annotation. The actual execution is delegated to the Cucumber runner (TestNG or JUnit).

Java
package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepDefinitions"},
    plugin = {"pretty", "html:target/report.html"},
    monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
    // No code here — Cucumber handles execution
}

Key Responsibilities

ResponsibilityHow
Link feature filesfeatures = "path/to/features"
Link step definitionsglue = {"stepDefinitions", "hooks"}
Configure reportingplugin = {"html:...", "json:..."}
Filter scenariostags = "@smoke"
Dry run checkdryRun = true

TestNG vs JUnit Runner

With TestNG:

Java
import io.cucumber.testng.AbstractTestNGCucumberTests;
@CucumberOptions(...)
public class TestRunner extends AbstractTestNGCucumberTests {}

With JUnit 4:

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

@RunWith(Cucumber.class)
@CucumberOptions(...)
public class TestRunner {}

With JUnit 5 (modern):

Java
import org.junit.platform.suite.api.*;
@Suite
@IncludeEngines("cucumber")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "stepDefinitions")
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "src/test/resources/features")
class TestRunner {}

Execution Flow

CODE
TestRunner (entry point)
    ↓ reads @CucumberOptions
    ↓ finds .feature files
    ↓ loads step definitions from glue package
    ↓ matches Gherkin steps to Java methods
    ↓ executes test scenarios
    ↓ generates reports via plugins

Follow AutomateQA

Related Topics