How can I run Cucumber test using explicit Maven goal?

12,264

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.

Share:
12,264
Fayez Alrafeea
Author by

Fayez Alrafeea

A software Engineer, love Java, Big Data, and Nutella :)

Updated on June 05, 2022

Comments

  • Fayez Alrafeea
    Fayez Alrafeea almost 2 years

    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?

  • Fayez Alrafeea
    Fayez Alrafeea over 8 years
    Thanks Di Matteo, but the problem with Cucumber is that its always executed because of the runner tag: @RunWith(Cucumber.class), it does not need any plugin inside pom.xml to run, is there any way to prevent that and control it from pom.xml?
  • A_Di-Matteo
    A_Di-Matteo over 8 years
    But you can skip/filter the classes/test cases using the @RunWith via the Surefire filtering. It will be applied by Maven when building, your IDE though (i.e. Eclipse) will keep on executing them
  • Fayez Alrafeea
    Fayez Alrafeea over 8 years
    Thanks Di Matteo, I was able to do that by extending the run method from Cucumber class and check there for a parameter passed at run time to decide whether to run the Cucumber test (by calling the super.run) or not (by doing nothing). I did the same for JBehave by extending JUnitStory class.