Run Junit Suite using Maven Command

杀马特。学长 韩版系。学妹 提交于 2019-12-17 22:31:38

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/11762801/run-junit-suite-using-maven-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!