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)
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
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
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
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)
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.