Enroll in Selenium Training

For successful implementation of any testing framework, it is mandatory that test cases can be run in multiple ways so that people with different competency levels can use it how they need to. Because running any test framework from the Terminal has its own advantages, such as overriding the run configurations mentioned in the code. So, now we will focus on various ways to run the Cucumber tests.

Run Cucumber Test from Command Line / Terminal

There are different ways to run Cucumber Test from command line. Tests can be run by using JUnit and Maven as well. But maven is the most suggested way and has extra benefits to it. This is why we started this Project as Maven project. And remember, Maven has a lot of advantages over other build tools, such as dependency management, lots of plugins and the convenience of running integration tests. Maven will allow our test cases to be run in different flavors, such as from the Terminal, integrating with Jenkins, and parallel execution.

Maven Project

If in case you directly landed to this tutorial from search and not following this complete Selenium Cucumber Framework series, I suggest you to go through the first chapter of End 2 End Selenium Test . This chapter covers all the prerequisite for running test from Maven. Also to make sure that you have the below mentioned dependencies added in your Maven Project:

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ToolsQA</groupId>
  <artifactId>CucumberFramework</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>CucumberFramework</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
	 <dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.12</version>
	    <scope>test</scope>
	 </dependency>
     <dependency>
	    <groupId>org.seleniumhq.selenium</groupId>
	    <artifactId>selenium-java</artifactId>
	    <version>3.7.0</version>
	</dependency>
	<dependency>
	    <groupId>info.cukes</groupId>
	    <artifactId>cucumber-java</artifactId>
	    <version>1.2.5</version>
	</dependency>
	<dependency>
	    <groupId>info.cukes</groupId>
	    <artifactId>cucumber-jvm-deps</artifactId>
	    <version>1.0.5</version>
	    <scope>provided</scope>
	</dependency>
	<dependency>
	    <groupId>info.cukes</groupId>
	    <artifactId>cucumber-junit</artifactId>
	    <version>1.2.5</version>
	    <scope>test</scope>
	</dependency>
	<dependency>
	    <groupId>info.cukes</groupId>
	    <artifactId>cucumber-picocontainer</artifactId>
	    <version>1.2.5</version>
	    <scope>test</scope>
	</dependency>
	<dependency>
	    <groupId>com.aventstack</groupId>
	    <artifactId>extentreports</artifactId>
	    <version>3.1.2</version>
	</dependency>
	<dependency>
	    <groupId>com.vimalselvam</groupId>
	    <artifactId>cucumber-extentsreport</artifactId>
	    <version>3.0.2</version>
	</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 cd until the project root directory.

Run Cucumber Test from Command Line / Terminal

  1. First, let's run all the Cucumber Scenarios from the command prompt. Since it's a Maven project and we have added Cucumber in test scope dependency and all features are also added in src/test packages, run the following command in the command prompt: mvn test

Running Cucumber Test from Command Line 6

You would notice below that it actually triggered the TestRunner file.

Running Cucumber Test from Command Line 6

Build Success Output

Running Cucumber Test from Command Line 6

Note: mvn test runs Cucumber Features using Cucumber's JUnit Runner. The @RunWith (Cucumber.class) annotation on the TestRunner class tells JUnit to kick off 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.

On the other hand, if you run test from eclipse when you use the JUnit Runner, these options are generated from the @CucumberOptions annotation on your test.

Overriding Cucumber Options

At times, you would need to override the configurations specified in @CucumberOptions. If we want to override the configurations mentioned in the Runner, then we need to use following command: mvn test –DCucumber.options="Your Options"

If you need help on these Cucumber options, then enter the following command in the command prompt and look at the output: mvn test -Dcucumber.options="--help"

Running Cucumber Test from Command Line 6

Note :This gives you the list of parameters that can be passed through command line using CucumberOptions.

Running a Scenario from Command Line

If we want to run single Scenario from the cmd, this is how we specify : mvn test -Dcucumber.options="feature file path" + "line number of the scenario"

Running Cucumber Test from Command Line 6

NoteIn the preceding code, "5" is the Feature file line number where a Scenario starts.

Running Scenarios using Tags from Command Line

If you want to run the test cases associated to Tags, this is how we specify : mvn test -Dcucumber.options="--tags @tag Name"

Running Cucumber Test from Command Line 6

Overriding Report Plugin and Path

If you want to generate a different report or if you wish to change the report path. This how you do it: mvn test -Dcucumber.options="--plugin junit:target/cucumber-reports/report.xml".

Please look at our Cucumber Report to see what all reports you can generate using @CucumberOptions.

Running a Feature file only from Command Line

If you want cucumber to run just a single feature file or multiple feature file, you can pass parameter for the same from command line. This how you do it:

mvn test -Dcucumber.options="src/test/resources/functionalTests/End2End_Tests.feature"

Passing multiple Parameter at once

Its also possible to pass multiple options at once. This is how it can be done mvn test -Dcucumber.options="SomeThing" -Dcucumber.options="SomeThing" -Dcucumber.options="SomeThing"

mvn test -Dcucumber.options="src/test/resources/functionalTests/End2End_Tests.feature" -Dcucumber.options="--tags @Smoke"

Cucumber Extent Report
Cucumber Extent Report
Previous Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback