Run Cucumber Test from Maven Command Line


To have a successful and effective implementation of a test framework, it is always advisable that the test framework supports test execution in multiple ways.
The 2 most commonly used ways to executes tests in Cucumber Framework is by running the tests using JUnit and Maven.

To execute tests using JUnit, we need to create a JUnit TestRunner. Whereas we need a Maven project to execute Cucumber tests from Command Line.

Create a Maven project and add the below mention dependencies in your Maven project.

 <!-- -->

<!-- -->

<!-- -->

<!-- -->


Run Test from Command Line

1. Open the command prompt and change directory to the project location where pom.xml is present.

cd C:\Users\Vibha\eclipse-workspace-test\demo

2. All feature files should be in src/test/resources and create Cucumber Runner class as CucumberRunnerTest.
Note:- The Runner class name should end with Test to execute the tests from Command Line
Run the following command in the command prompt:

mvn test

mvn test runs Cucumber Features using Cucumber’s JUnit Runner. The @RunWith (Cucumber.class) annotation on the TestRunner class tells JUnit to start Cucumber. Cucumber run time parses the command-line options to know what Feature to run, where the Glue Code lives, what plugins to use, and so on.

3. Below screenshot shows that CucumberRunnerTest class is triggered.

4. Below screenshot shows that build success output.

Overriding Cucumber Options

Cucumber provides several options that can be passed to on the command-line.

1. Running Scenarios using Tags from Command Line

If you are using Maven and want to run a subset of scenarios tagged with @ValidCredentials.

mvn test -Dcucumber.filter.tags="@ValidCredentials"

There is one more way to execute Cucumber tests with tags

mvn test -Dcucumber.options="--tags @ValidCredentials"

2. Running a Feature file from Command Line

Suppose you want to run single Feature File from the command line, then use the below syntax

mvn test -Dcucumber.options="src/test/resources/features/CucumberTagsExample.feature"

3. Creating Cucumber Report from Command Line

If we want to generate a different report, then we can use the following command and see the JUnit report generate at the location mentioned:

mvn test -Dcucumber.options="--plugin junit:target/cucumber-reports/cucumberReport.xml

4. Passing multiple Parameter from Command Line

If we want to pass more than one parameters, then we can use the following command

mvn test -Dcucumber.options="src/test/resources/features/CucumberTagsExample.feature" -Dcucumber.filter.tags="@InValidCredentials"

5. Running a Scenario without tag from Command Line

If we want to run single Scenario from the command line and no tag is assigned to that scenario, this is how we specify

 mvn test -Dcucumber.options="src/test/resources/features/CucumberTagsExample.feature:12"

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

In the next tutorial, I have explained to run Cucumber Gradle tests from Command Line.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s