Run Junit Suite using Maven Command
Asked Answered
S

2

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.

Sharleensharlene answered 1/8, 2012 at 15:40 Comment(5)
I'm not sure if you can run JUnit test suite from surefire plugin.Darendaresay
Still does not work used as: mvn test -Dtest=com.org.mysuites.FastTestSuite -DfailIfNoTests=falseSharleensharlene
You can run using surefire plugin that is working if you include it e.g.<include>**/FastTestSuite.class</include>. Its just a requirement to run it from command line using maven command.Sharleensharlene
No got an exception running mvn clean test -Dtest=FastTestSuite because its not picking up that suite so there is not test to run.Sharleensharlene
Just for info that all suites are under src/test/java/ (not sure if it matters).Sharleensharlene
S
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
Sharleensharlene answered 2/8, 2012 at 12:8 Comment(4)
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.Crews
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, maven.apache.org/surefire/maven-surefire-plugin/examples/…).Yuriyuria
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.Nonetheless
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 (<runSuite> ... </runSuite>) to the existing one.Fireworm
I
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.

Inception answered 1/8, 2012 at 22:5 Comment(2)
As I said in my question that I need to run this using maven command. It is working the way you mentioned. I dont want to add profile basically.Sharleensharlene
Indeed, profile management IS the way to trigger different behaviour.Killer

© 2022 - 2024 — McMap. All rights reserved.