Ronnie
Ronnie

Reputation: 251

How to run single cucumber feature files through command prompt and through jenkins using Maven?

I am bit new to Cucumber / Maven, so require help on running test cases. I have developed an automation suite in eclipse using Cucumber and Selenium. To run specific feature files / Junit runner class, I right-click on the files in Eclipse and run it.

But how do I run it through command prompt or Jenkins by giving specific commands to run 2-3 feature files (or) 2-3 Junit runner classes out of say 50 feature files or JUnit classes?

Below is the package explorer of how I have structured in Eclipse.

enter image description here

Below is the POM.xml

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

    <groupId>com.perspecsys</groupId>
    <artifactId>salesforce</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>salesforce</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.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>

    </dependencies>
</project>

Upvotes: 19

Views: 81117

Answers (5)

You can run a single feature file by using cucumber.options which will override all the options you have in the @CucumberOptions annotation:

mvn test -Dcucumber.options="src/test/features/com/perspecsys/salesforce/featurefiles/Account.feature"

EDIT (Jun 2021): Subsequent versions removed the cucumber.options construct and replaced it. One way to achieve the same result now is through the use of tags. Place a tag on top of your feature file (example @feature_file_name) and then run the following command:

mvn test -Dcucumber.filter.tags='@feature_file_name'

Upvotes: 30

Roman Denysenko
Roman Denysenko

Reputation: 79

I had some problems with Maven execution and as I understood that most of all people are using Terminal but only one (@Pedro Lopez) described a very important point - passing 'CucumberRunner' class in Maven command when you will execute it using console, jenkins or something else.

At the same time, it's possible to do using POM:

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <includes>
                    <exclude>
                        **/CucumberRunner.java
                    </exclude>
                </includes>

Here is **/CucumberRunner.java - class that you will use as runner for passing all Cucumber options (@William mentioned about 'RunCukesTest' in my POM is CucumberRunner ) and of course you can use simple command like mvn test after POM configuration.

Regarding to second part of the question, I found a very simple solution. Here is possible to describe all in the Cucumber option section.

@CucumberOptions(
        features = feature", // here you can pass information about main feature folder that contains all feature files
        plugin = "junit:cucumber-report/junit-report.xml",
        tags = "@Test1, @Test2, ... , @Test999",//here you can pass all list of related tests by tag
        glue = {"com.myApp.stepDefinitions"}
)

I think that this information can help to solve some problems in the future :)

happy automating with JAVA!

Upvotes: 1

user6939
user6939

Reputation: 363

You can run a single feature file by using cucumber.options

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

Upvotes: 10

William
William

Reputation: 549

If you wanted it hard coded, you could do something like:

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features={"src/test/resources/features/belly.feature"}  
        ,glue =  "steps"   //whatever the name of your steps package is
        )
public class RunCukesTest 
{
}

Upvotes: 3

Pedro Lopez
Pedro Lopez

Reputation: 2276

Given that you are using Maven and that you have a separate runner class per feature already, you can just use the Failsafe plugin or the Surefire plugin to pass a parameter from command line like this, and select which runner to run.

Failsafe version:

-Dit.test=Account*Test,Login*Test

Surefire version:

-Dtest=Account*Test,Login*Test

Note that you can use wildcards, pass a comma-separated list of tests and reference the full location and filter by package.

-Dit.test=com.perpecsys.salesforce.*Test

For this I would normally use the Failsafe plugin, as the Cucumber tests a more like integration tests (end-to-end tests really), and that is what that plugin is for, whereas the Surefire plugin is designed to run unit tests.

More information on both plugins:

http://maven.apache.org/surefire/maven-failsafe-plugin/examples/single-test.html

http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

From Jenkins it is exactly the same, just use the Maven Project plugin to create a new Maven Project and in the Build section, under the Goal and options field specify the maven command like:

clean verify -Dit.test=Account*Test

https://wiki.jenkins-ci.org/display/JENKINS/Maven+Project+Plugin

Alternatively, just add the Invoke top-level maven targets step to the Build if you are not using the Maven Project Plugin, and add the options there.

Hope this helps.

Upvotes: 2

Related Questions