Run Cucumber Test from Maven Command Line

HOME

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 execute tests in Cucumber Framework are by running the tests using JUnit and Maven.

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

Create a Maven project and add the below-mentioned dependencies to your Maven project.

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

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

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>

<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.10.0</version>
      <scope>test</scope>
     </dependency>

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

Run Test from Command Line

1. Open the command prompt and change the 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 the 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 runs 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. The below screenshot shows that CucumberRunnerTest class is triggered.

4. The below screenshot shows the 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 a 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 parameter, 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 a tag from Command Line

If we want to run a 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 explained to run Cucumber Gradle tests from Command Line.

4 thoughts on “Run Cucumber Test from Maven Command Line

    1. I have tried and we can run a feature file from commandline with version 7.3.1 using same command : mvn test -Dcucumber.options=”src/test/resources/features/Login.feature”. To run a particular tag, this command works : mvn test -Dcucumber.filter.tags=”@InvalidCredentials”

      Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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