Test Runner in Cucumber using TestNG

  

What is a Test Runner Class in Cucumber?

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@AfterClass etc.).


Key Responsibilities of a Test Runner Class

  1. Specify the location of feature files

  2. Specify the location of step definitions

  3. Configure other Cucumber options like plugins, tags, dryRun, etc.

  4. Use @CucumberOptions to customize execution

  5. Extend AbstractTestNGCucumberTests when using TestNG



Implementing Test Runner Class Using TestNG

Folder Structure Example:

project-root/
├── src/test/java/
│   ├── features/
│   │   └── login.feature
│   ├── stepDefinitions/
│   │   └── LoginSteps.java
│   └── runner/
│       └── TestRunner.java



Below are steps to Implement Test Runner in Cucumber using Testng 


1. Add Maven dependencies in 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>



2. Write the Feature File (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



3. Write Step Definitions (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");
    }
}




4. Create Test Runner Class (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.xml

  • Group and prioritize tests

  • Run parallel tests using TestNG support

  • Better integration with reporting tools and Jenkins



Important Points:

PropertyDescription
featuresPath to .feature files
gluePackage for step definitions and hooks
pluginReporting plugins (e.g., HTML, JSON, pretty)
monochromeDisplay readable console output
dryRuntrue to check if step definitions exist for each step, without executing

No comments:

Post a Comment