跳过Maven中某些模块的测试

Jos*_*Fox 65 junit build-process maven-2 unit-testing maven

我希望我的Maven构建版能够运行大多数单元测试.但是在一个项目中有单元测试速度较慢,我想一般排除它们; 并偶尔打开它们.

问题:我该怎么做?

我知道-Dmaven.test.skip=true,但这关闭了所有单元测试.

我也知道跳过集成测试,这里描述.但我没有集成测试,只有单元测试,我没有任何显式调用maven-surefire-plugin.(我正在使用带有Eclipse-Maven插件的Maven 2).

Rom*_*las 79

那么只在这个模块中跳过测试呢?

在这个模块的pom.xml中:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.4.2</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)

最后,您可以创建一个禁用测试的配置文件(仍然是模块的pom.xml):

<project>
  [...]
  <profiles>
    <profile>
      <id>noTest</id>
      <activation>
        <property>
          <name>noTest</name>
          <value>true</value>
        </property>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.2</version>
            <configuration>
              <skipTests>true</skipTests>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)

使用后一种解决方案,如果运行mvn clean package,它将运行所有测试.如果运行mvn clean package -DnoTest=true,它将不会运行此模块的测试.

  • 对于其他用户,另一种选择是跳过整个模块,直到您要构建并验证它为止:http://stackoverflow.com/a/5542779/543935 (2认同)
  • 以下是如何创建仅在激活时运行测试的 maven 配置文件:http://stackoverflow.com/questions/34334257/why-maven-runs-profile-plugin-even-if-profile-is-not-已激活/41808680#41808680 (2认同)

Lui*_*Rol 30

我认为这更容易,并且还具有为非确定性测试工作的好处(在我的情况下,FlexUnitTests)

<profile>
   <id>noTest</id>
    <properties>
       <maven.test.skip>true</maven.test.skip>
    </properties>
 </profile>
Run Code Online (Sandbox Code Playgroud)

  • 这是因为你应该使用-PnoTest而不是-DnoTest = true (4认同)
  • 将 maven.test.skip 设置为 true 还会产生跳过测试编译的效果,因为skipTests 仍然编译测试但不运行它们。 (4认同)

A_D*_*teo 6

如果您有一个大型的多模块项目,并且只想跳过某些模块中的测试,而无需pom.xml使用自定义配置和配置文件来更改每个模块文件,则可以将以下内容添加到父pom.xml文件中:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>regex-property</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>maven.test.skip</name>
                        <value>${project.artifactId}</value>
                        <regex>(module1)|(module3)</regex>
                        <replacement>true</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<modules>
    <module>module1</module>
    <module>module2</module>
    <module>module3</module>
</modules>
Run Code Online (Sandbox Code Playgroud)

多亏了build-helper-maven-plugin您,您实际上可以在构建期间通过project.artifactId属性(指向artifactId构建期间指向每个模块)动态地检查您是否在某个模块中,正则表达式将查找某些值的匹配项(您为其指定的模块名称)要跳过测试),并相应地填充maven.test.skip属性(将其设置为true)。

在这种情况下,测试将被跳过,module1并且module3在为正确运行时module2,即由正则表达式表示。

这种方法的优点是使它动态且集中(在父级中pom.xml),从而更好地进行维护:您可以随时通过更改上述简单的正则表达式随时添加或删除模块。

显然,如果这不是构建的默认行为(建议的情况),则始终可以将上面的代码段包装在maven配置文件中


您还可以根据自己的输入,采取更动态的行为:

<properties>
    <test.regex>none</test.regex>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.12</version>
            <executions>
                <execution>
                    <id>regex-property</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>maven.test.skip</name>
                        <value>${project.artifactId}</value>
                        <regex>${test.regex}</regex>
                        <replacement>true</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

在这里,我们实际上是将regex值替换为属性,test.regex并将其默认值设置为none(或任何不匹配任何模块名称的值,或者也将是所需的默认跳过匹配项)。

然后从命令行我们可以

mvn clean test -Dtest.regex="(module1)" > will skip tests only for module1
mvn clean test -Dtest.regex="(module1)|(module2)" > will skip tests on module1 and module2
mvn clean test -Dtest.regex="(module1)|(module2)|(module3)" > will skip the three module tests
mvn clean test -Dtest.regex=".+" > will skip all module tests
mvn clean test > would not skip anything (or fall back on default behavior)
Run Code Online (Sandbox Code Playgroud)

也就是说,在运行时,您无需更改pom.xml文件或激活任何配置文件就可以决定。