How to run cucumber-jvm tests with maven

55,687

Solution 1

Your test classes need to end with the Test suffix.

Solution 2

My preferred way to run Cucumber JVM tests from Maven is to bridge them through JUnit by providing a RunCukesTest class that triggers Cucumber from JUnit. I find this easier and with more control than using a specific Cucumber plugin for Maven. Also, this is more independent of the build tool. Most build tools support JUnit out-of-the-box without any further configuration. Running cucumber directly from the build tool would require to use a plugin and configure it.

Running Cucumber via JUnit, as shown below, works not only with Maven but also with Gradle.

However, note that this runs your Cucumber tests with the same run as the JUnit tests. This may or may not be what you want. For test execution performance reasons you may want to keep unit/developer tests, integration/architecture tests, and acceptance/user tests in separate executions. But even if you separate them, I recommend to see the selection of the test framework orthogonal to the test automation pyramid layers. Which means, you may change the name of RunCukesTest to, say, RunCukesIT or whatever naming scheme you use for selection, or place them in a different path. That it's easier to

A typical RunCukesTest class can look like this:

package com.testing;

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

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources/features",
        glue = "com.testing"
)
public class RunCukesTest {
}

Explanation of the details:

  • @RunWith(Cucumber.class) tells JUnit to not run this test class with it's default runner, but instead with Cucumber which is a test runner for JUnit4 that runs Cucumber from within JUnit.
  • @CucumberOptions tells Cucumber where to find its stuff. The main things are:
    • features tells Cucumber where to find the feature files.
    • glue tells Cucumber which packages contain the step definitions.

Note that above example is for the situation where the tests written in Cucumber are orthogonal to your package architecture. For simple projects, an easier setup is possible: Put the feature files and the step definitions in the same directory/package within src/resources as the RunCukesTest in src/java, and remove the @CucumberOptions annotation.

Combining Cucumber with JUnit5 is still a bit tricky and cumbersome. I will update this answer with JUnit5 once I find it practical.

Solution 3

By maven standards tests should be in src/test/java and not in src/main/java. Your pom.xml looks fine to me. I suspect if you import your tests and resources from main to test folder, your tests should execute just fine.

Share:
55,687
user3188928
Author by

user3188928

Updated on July 09, 2022

Comments

  • user3188928
    user3188928 almost 2 years

    How do I run the cucumber tests that I have in the following locations with Maven.

    Source folders 'src/main/java' and 'src/main/resources' include step definitions and feature files in package 'com.testing.TestProject.login' created in each source folder.

    I have included plugins and dependencies in the POM file but when I run maven phase integration-test, cucumber tests are not getting executed.

    I'm new to Maven. Please let me know what to include in the POM file to run the cucumber tests with Maven.

    Here is my POM file:

       <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.testing</groupId>
      <artifactId>MyMavenProject</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>MyMavenProject</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <cucumber-jvm.version>1.1.5</cucumber-jvm.version>
        <selenium.version>2.39.0</selenium.version>
        <junit.version>4.11</junit.version>
      </properties>
    
        <build>
    
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executableDependency>
                        <groupId>info.cukes</groupId>
                        <artifactId>cucumber-core</artifactId>
                    </executableDependency>
                    <mainClass>cucumber.api.cli.Main</mainClass>
                    <arguments>
                        <argument>--format</argument>
                        <argument>junit:target/cucumber-junit-report/allcukes.xml</argument>
                        <argument>--format</argument>
                        <argument>pretty</argument>
                        <argument>--format</argument>
                        <argument>html:target/cucumber-html-report</argument>
                        <argument>--tags</argument>
                        <argument>@login</argument>
                        <argument>--glue</argument>
                        <argument>com/</argument>
                        <argument>src/</argument>
                    </arguments>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>info.cukes</groupId>
                        <artifactId>cucumber-core</artifactId>
                        <version>1.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>  
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>${cucumber-jvm.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber-jvm.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.7</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.3</version>
    </dependency>
    </dependencies>
    </project>
    

    Thanks in advance.

  • user3188928
    user3188928 about 10 years
    I tried moving tests to src/test/java but still the tests are not getting executed.
  • nilesh
    nilesh about 10 years
    What is the glue parameter and its values com/,src/. Also do you have @login tag in your test? Do you get any error or the tests just don't run?
  • user3188928
    user3188928 about 10 years
    glue parameters is the PATH Where step definitions and hooks is loaded from. yes i have @login tag in the tests. i have tried removing the glue parameters as well as tag also. still cucumber tests are not getting executed.
  • user3188928
    user3188928 about 10 years
    no errors when i run tests. but the following warning message is shown [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ CucumberTestProject --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:\MavenRep\MyCucumberTestProject\CucumberTestProject\src\ma‌​in\resources
  • user3188928
    user3188928 about 10 years
    I figured the issue. My runner class was not ended with test. when I changed runner class name to suiterunnertest from suiterunner, tests started executing. Thanks nilesh for your help
  • Fabio Araujo
    Fabio Araujo over 8 years
    On the class I have @RunWith(Cucumber.class) annotation I have inserted the "Test" suffix and it resolved my issues
  • Adam Howell
    Adam Howell almost 6 years
    Thank you for for this answer, Sergiu. I would like to add that it cannot end in "Tests" (plural), nor "test" (lower case).