Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

maven: How can I skip test in some projects via command line options?

In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?

My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.

I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.

If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.

To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <excludes>
            <exclude>${someModule.test.excludes}</exclude>
          </excludes>
          <includes>
            <include>${someModule.test.includes}</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <someModule.skip.tests>false</someModule.skip.tests>
    <skipTests>${someModule.skip.tests}</skipTests>
    <someModule.test.includes>**/*Test.java</someModule.test.includes>
    <someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
  </properties>

With a POM like the above you can execute tests in a variety of ways.

  1. Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
  1. Skip all tests across all modules
mvn -DskipTests=true test
  1. Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
  1. Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
  1. Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...