What is a Test Runner Class in Cucumber?
A Test Runner class in Cucumber is used to execute feature files and link them with step definition code. It acts as a bridge between:
Feature files (written in Gherkin syntax) and
Step Definition classes (Java methods mapped to steps).
In the context of Cucumber with TestNG, the Test Runner allows us to run scenarios just like regular TestNG test cases, and we get benefits like:
Parallel execution,
TestNG XML suite configuration,
Integration with TestNG annotations (
@BeforeClass,@AfterClassetc.).
Key Responsibilities of a Test Runner Class
Specify the location of feature files
Specify the location of step definitions
Configure other Cucumber options like plugins, tags, dryRun, etc.
Use
@CucumberOptionsto customize executionExtend
AbstractTestNGCucumberTestswhen using TestNG
pom.xml:<dependencies> <!-- Cucumber dependencies --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.15.0</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>7.15.0</version> </dependency> <!-- TestNG dependency --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.10.2</version> <scope>test</scope> </dependency> </dependencies>
login.feature)Feature: Login Functionality Scenario: Successful login with valid credentials Given user is on login page When user enters valid username and password Then user should be redirected to the homepage
LoginSteps.java)package stepDefinitions; import io.cucumber.java.en.*; public class LoginSteps { @Given("user is on login page") public void user_is_on_login_page() { System.out.println("User is on login page"); } @When("user enters valid username and password") public void user_enters_valid_credentials() { System.out.println("User entered username and password"); } @Then("user should be redirected to the homepage") public void user_should_see_homepage() { System.out.println("User is on homepage"); } }
TestRunner.java)package runner; import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; @CucumberOptions( features = "src/test/java/features", glue = "stepDefinitions", plugin = { "pretty", "html:target/cucumber-reports.html", "json:target/cucumber.json" }, monochrome = true ) public class TestRunner extends AbstractTestNGCucumberTests { }
How to Run It?
Option 1: Run TestRunner.java directly from your IDE (Eclipse/IntelliJ)
Option 2: Add TestNG XML to run from command line or CI
<suite name="Cucumber Test Suite"> <test name="Cucumber Scenarios"> <classes> <class name="runner.TestRunner" /> </classes> </test> </suite>
Advantages of Using TestNG with Cucumber:
Easily manage test suites using
testng.xmlGroup and prioritize tests
Run parallel tests using TestNG support
Better integration with reporting tools and Jenkins
| Property | Description |
|---|---|
| features | Path to .feature files |
| glue | Package for step definitions and hooks |
| plugin | Reporting plugins (e.g., HTML, JSON, pretty) |
| monochrome | Display readable console output |
| dryRun | true to check if step definitions exist for each step, without executing |