How can I run Cucumber test using explicit Maven goal?

Fayez Alrafeea picture Fayez Alrafeea · Jan 3, 2016 · Viewed 10.7k times · Source

I have an application contains both Cucumber and JBehave test, I want to be able to run one of them optionally every time, I can do that with JBehave by explicit Maven goal, but the problem is that Cucumber run implicitly with each build or test, is there anyway to stop and run it o choice?

Answer

A_Di-Matteo picture A_Di-Matteo · Jan 3, 2016

You can achieve this by configuring the Maven Surefire Plugin as part of your default build or/and via a profile.

If your Maven build section, you can skip the Cucumber tests by default (given that they either have all the same suffix or belong all to the same package, alternatively you can arrange them to meet any of these two criterias):

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <excludes>
             <!-- classes that include the name CucumberTest, as an example -->
             <exclude>**/*CucumberTest*.java</exclude>
             <!-- classes in a package whose last segment is named cucumber, as an example -->
             <exclude>**/cucumber/*.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

As such, Maven by default (as part of the default build) will skip your Cucumber tests.

Then, you can configure a Maven Profile to run exclusively the Cucumber tests with a counterpart of the Maven Surefire Plugin configuration as following:

<project>
  [...]
  <profiles>
     <profile>
          <id>cucumber-tests</id>
          <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <excludes>
                        <exclude>none</exclude>
                    </excludes>
                    <includes>
                        <!-- Include your Cucumber tests, as an example -->
                        <include>**/*CucumberTest.java</include>
                    </includes>
                </configuration>
              </plugin>
            </plugins>
          </build>
    <profile>
  </profiles>
  [...]
</project>

Then running mvn clean install -Pcucumber-tests will run your Cucumber tests.

This approach would give you more flexibility on configuration in both scenarios (default or cucumber tests build) and you could swap the behavior according to your needs.

Alternatively, for a simpler (but less flexible) approach, you could follow the suggestion on another SO answer and use a Maven property to have a switch cucumber tests on/off as following:

<properties>
  <exclude.cucumber.tests>nothing-to-exclude</exclude.cucumber.tests>
</properties>

<profiles>
  <profile>
    <id>exclude-cucumber</id>
    <properties>
      <exclude.cucumber.tests>**/*Cucumber*.java</exclude.cucumber.tests>
    </properties>
  </profile>
</profiles>

<build>
    <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <excludes>
             <exclude>${exclude.cucumber.tests}</exclude>
            </excludes>
          </configuration>
        </plugin>
    </plugins>
</build>

Using the configuration above, by default Maven will execute Cucumber tests and skip them when executing mvn clean install -Pexclude-cucumber (the profile will change the content of the exclude.cucumber.tests property and as such change the Surefire plugin filter). You can of course swap the behavior is as well and have an include-cucumber profile instead.