40

I have multiple Junit test suites (SlowTestSuite, FastTestSuite etc). I would like to run only specific suite using maven command. e.g.

mvn clean install test -Dtest=FastTestSuite -DfailIfNoTests=false

but its not working. Just not running any test at all. Any suggestions please.

Java SE
  • 2,073
  • 4
  • 19
  • 25
  • I'm not sure if you can run JUnit test suite from surefire plugin. – Andrew Logvinov Aug 01 '12 at 15:46
  • Still does not work used as: mvn test -Dtest=com.org.mysuites.FastTestSuite -DfailIfNoTests=false – Java SE Aug 01 '12 at 15:46
  • 2
    You can run using surefire plugin that is working if you include it e.g.`**/FastTestSuite.class`. Its just a requirement to run it from command line using maven command. – Java SE Aug 01 '12 at 15:50
  • No got an exception running `mvn clean test -Dtest=FastTestSuite` because its not picking up that suite so there is not test to run. – Java SE Aug 01 '12 at 15:53
  • Just for info that all suites are under src/test/java/ (not sure if it matters). – Java SE Aug 01 '12 at 15:59

2 Answers2

54

I have achieved this by adding property into pom as:

<properties>
    <runSuite>**/FastTestSuite.class</runSuite>
</properties>

and maven-surefire-plugin should be:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>${runSuite}</include>
                </includes>
            </configuration>
        </plugin>

so it means by default it will run FastTestSuite but you can run other test e.g. SlowTestSuite using maven command as:

mvn install -DrunSuite=**/SlowTestSuite.class -DfailIfNoTests=false
Java SE
  • 2,073
  • 4
  • 19
  • 25
  • I would like to add that the maven-surefure-plugin is a requirement, I found in some places saying that only with the property it'll work, but is not true. – TheGabiRod Jun 01 '16 at 13:54
  • 3
    Thanks **@Java SE**, your approach works perfectly with *Maven 3.3.3* and *JUnit 4.12*. But `**/FastTestSuite.java` can be used instead of `**/FastTestSuite.class`. Both variants perform fine, but specifying `.java` looks more consistent with official documentation (for instance, http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html). – flaz14 Jul 13 '16 at 12:42
  • When I add these settings my test suite runs but all other tests are excluded. If I don't add the settings all tests run but my test suite is ignored. – David DeMar Nov 08 '16 at 15:53
  • Minor addenda -- the `properties` element goes at the root of your `project` element (i.e. directly inside it, without being inside anything that's also inside `project`, a sibling of `dependencies` and `build` and so on). If you already have a `properties` element in your `project` root, _do not_ add another -- instead, just add the inner line (` ... `) to the existing one. – Nic Aug 17 '18 at 21:55
5

The keyword you missed is maven-surefire-plugin :http://maven.apache.org/plugins/maven-surefire-plugin/.

Usage is :

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.1</version>
        <configuration>
          <includes>
            <include>**/com.your.packaged.Sample.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

If you make a little search on stack overflow, you may find information :

Running a JUnit4 Test Suite in Maven using maven-failsafe-plugin Using JUnit Categories with Maven Failsafe plugin

In addition, you may define profile, like fastTest, that will be triggered by adding parameter to cmd line :

mvn package -PfastTests

This profile would include some inclusions too.

Community
  • 1
  • 1
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65