Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Running Cucumber

Save for later
  • 6 min read
  • 03 Jun 2015

article-image

In this article by Shankar Garg, author of the book Cucumber Cookbook, we will cover the following topics:

  • Integrating Cucumber with Maven
  • Running Cucumber from the Terminal
  • Overriding options from the Terminal

(For more resources related to this topic, see here.)

Integrating Cucumber with Maven

Maven has a lot of advantages over other build tools, such as dependency management, lots of plugins and the convenience of running integration tests. So let's also integrate our framework with Maven. Maven will allow our test cases to be run in different flavors, such as from the Terminal, integrating with Jenkins, and parallel execution.

So how do we integrate with Maven? Let's find out in the next section.

Getting ready

I am assuming that we know the basics of Maven (the basics of Maven are out of the scope of this book). Follow the upcoming instructions to install Maven on your system and to create a sample Maven project.

  1. We need to install Maven on our system first. So, follow instructions mentioned on the following blogs:

    For Windows:

    http://www.mkyong.com/maven/how-to-install-maven-in-windows/

    For Mac:

    http://www.mkyong.com/maven/install-maven-on-mac-osx/

  2. We can also install the Maven Eclipse plugin by following the instructions mentioned on this blog:

    http://theopentutorials.com/tutorials/eclipse/installing-m2eclipse-maven-plugin-for-eclipse/.

  3. To import a Maven project into Eclipse, follow the instructions on this blog:

    http://www.tutorialspoint.com/maven/maven_eclispe_ide.htm.

How to do it…

Since it is a Maven project, we are going to change the pom.xml file to add the Cucumber dependencies.

  1. First we are going to declare some custom properties which will be used by us in managing the dependency version:
    <properties>
       <junit.version>4.11</junit.version>
       <cucumber.version>1.2.2</cucumber.version>
       <selenium.version>2.45.0</selenium.version>
       <maven.compiler.version>2.3.2</maven.compiler.version>
    </properties>
  2. Now, we are going to add the dependency for Cucumber-JVM with scope as test:
    <!—- Cucumber-java-->
    <dependency>
       <groupId>info.cukes</groupId>
       <artifactId>cucumber-java</artifactId>
       <version>${cucumber.version}</version>
       <scope>test</scope>
    </dependency>
  3. Now we need to add the dependency for Cucumber-JUnit with scope as test.
    <!-— Cucumber-JUnit -->
    <dependency>
       <groupId>info.cukes</groupId>
       <artifactId>cucumber-junit</artifactId>
       <version>${cucumber.version}</version>
       <scope>test</scope>
    </dependency>

That's it! We have integrated Cucumber and Maven.

How it works…

By following these Steps, we have created a Maven project and added the Cucumber-Java dependency. At the moment, this project only has a pom.xml file, but this project can be used for adding different modules such as Feature files and Step Definitions.

The advantage of using properties is that we are making sure that the dependency version is declared at one place in the pom.xml file. Otherwise, we declare a dependency at multiple places and may end up with a discrepancy in the dependency version.

The Cucumber-Java dependency is the main dependency necessary for the different building blocks of Cucumber. The Cucumber-JUnit dependency is for Cucumber JUnit Runner, which we use in running Cucumber test cases.

Running Cucumber from the Terminal

Now we have integrated Cucumber with Maven, running Cucumber from the Terminal will not be a problem. Running any test framework from the Terminal has its own advantages, such as overriding the run configurations mentioned in the code.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $15.99/month. Cancel anytime

So how do we run Cucumber test cases from the Terminal? Let's find out in our next section.

How to do it…

  1. Open the command prompt and cd until the project root directory.
  2. 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 test packages, run the following command in the command prompt:
    mvn test

    This is the output:

     running-cucumber-img-0

     running-cucumber-img-1

  3. The previous command runs everything as mentioned in the JUnit Runner class. However, if we want to override the configurations mentioned in the Runner, then we need to use following command:
    mvn test –DCucumber.options="<<OPTIONS>>"
  4. 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"

    This is the output:
    running-cucumber-img-2

How it works…

mvn test runs Cucumber Features using Cucumber's JUnit Runner. The @RunWith (Cucumber.class) annotation on the RunCukesTest class tells JUnit to kick off Cucumber. The Cucumber runtime parses the command-line options to know what Feature to run, where the Glue Code lives, what plugins to use, and so on. When you use the JUnit Runner, these options are generated from the @CucumberOptions annotation on your test.

Overriding Options from the Terminal

When it is necessary to override the options mentioned in the JUnit Runner, then we need Dcucumber.options from the Terminal. Let's look at some of the practical examples.

How to do it…

  1. If we want to run a Scenario by specifying the filesystem path, run the following command and look at the output:
    mvn test -Dcucumber.options= "src/test/java/com/features/sample.feature:5"

     running-cucumber-img-3

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

  2. If we want to run the test cases using Tags, then we run the following command and notice the output:
    mvn test -Dcucumber.options="--tags @sanity"

    The following is the output of the preceding command:

    running-cucumber-img-4

  3. 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-junit-report.xml"

    running-cucumber-img-5

How it works…

When you override the options with -Dcucumber.options, you will completely override whatever options are hardcoded in your @CucumberOptions. There is one exception to this rule, and that is the --plugin option. This will not override, but instead, it will add a plugin.

Summary

In this article we learned that 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. In this article, we also covered advanced topics of running Cucumber test cases in parallel by a combination of Cucumber and Maven.

Resources for Article:


Further resources on this subject: