Cucumber Test Execution

Cucumber test scripts can be executed in three ways:

     1. From the IDE (like IntelliJ or Eclipse)
     2. Using a Runner class (JUnit or TestNG)
     3. Through the command line with Maven

1. Run from the IDE

You can run scenarios directly from the .feature file.

     Open the .feature file
     Right-click on a Scenario: or use the green run icon
     Select Run

Note: This method is useful for quick test runs, but Cucumber reports will not be generated.


2. Run Using a Runner Class

For more advanced and customizable execution, use a Runner class.

This method allows:

     1. Running selected scenarios using tags
     2. Generating reports (HTML, JSON, etc.)
     3. Organizing test execution

Cucumber supports two types of runners:
     1. JUnit Runner
     2. TestNG Runner

Using JUnit Runner

Step 1: Add Cucumber-Junit dependency in pom.xml (we added it previously in this tutorial)

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.23.0</version>
<scope>test</scope>
</dependency>


Step 2: Create the Runner class

Inside src/test/java, create a new package named runners, and add a class named Runner.

Example JUnit runner class script

package test.utils;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
    plugin = {
        "pretty",
        "json:target/Reports/cucumber.json",
        "html:target/Reports/cucumber.html"
    },
    glue = "StepDefinitions",
    features = "src/main/resources",
    tags = "@login",
    monochrome = true
)
public class Runner {
}


Explanation of annotations:

     glue: Path to step definition classes
     features: Path to .feature files
     tags: Runs only scenarios with the specified tag
     plugin: Enables reporting (HTML, JSON)
     monochrome: Formats console output for better readability

To run: Right-click on the Runner class and choose Run 'Runner'.

Using TestNG Runner

Step 1: Add Cucumber - TestNG dependency in pom.xml

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.23.0</version>
</dependency>


Step 2: Create the Runner class

Inside src/test/java, create a package runners and add a class named Runner.

Example JUnit runner class script

package test.utils;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(
    plugin = {
        "pretty",
        "json:target/Reports/cucumber.json",
        "html:target/Reports/cucumber.html"
    },
    glue = "StepDefinitions",
    features = "src/main/resources",
    monochrome = true
)
public class Runner extends AbstractTestNGCucumberTests {
}


Make sure the runner class extends AbstractTestNGCucumberTests to work with TestNG.

3. Run Through Command Line (Maven)

We can execute the test in command line through maven surfire plugin if the workspace is created through maven. The maven plugin will trigger the Runner class Junit runner / Testng Runner to execute the test.

Step 1: Add Maven Surefire Plugin (if not already added)

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>


Step 2: Run with these Commands to Execute Cucumber Tests

Run all scenarios: mvn test

Run scenarios with a specific tag: mvn test -Dcucumber.filter.tags="@ValidCredentials"

Run a specific feature file: mvn test -Dcucumber.options="src/main/resources/login.feature"

Make sure the runner class is in src/test/java so the Maven test phase picks it up.
 

Related Tutorials