





















































In this article by Shankar Garg, author of the book Cucumber Cookbook, we will cover the following topics:
(For more resources related to this topic, see here.)
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.
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.
For Windows:
http://www.mkyong.com/maven/how-to-install-maven-in-windows/
For Mac:
http://theopentutorials.com/tutorials/eclipse/installing-m2eclipse-maven-plugin-for-eclipse/.
Since it is a Maven project, we are going to change the pom.xml file to add the Cucumber dependencies.
<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>
<!—- Cucumber-java--> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>${cucumber.version}</version> <scope>test</scope> </dependency>
<!-— 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.
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.
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.
So how do we run Cucumber test cases from the Terminal? Let's find out in our next section.
mvn test
This is the output:
mvn test –DCucumber.options="<<OPTIONS>>"
mvn test -Dcucumber.options="--help"
This is the output:
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.
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.
mvn test -Dcucumber.options= "src/test/java/com/features/sample.feature:5"
In the preceding code, "5" is the Feature file line number where a Scenario starts.
mvn test -Dcucumber.options="--tags @sanity"
The following is the output of the preceding command:
mvn test -Dcucumber.options= "--plugin junit:target/cucumber-junit-report.xml"
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.
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.
Further resources on this subject: