junit 在Maven中的某些模块中跳过测试

jtoj6r0c  于 2023-03-23  发布在  Maven
关注(0)|答案(6)|浏览(229)

我希望我的Maven版本能够运行大多数单元测试,但是有一个项目中的单元测试比较慢,我希望一般情况下排除它们;偶尔打开它们。

问题:我该怎么做呢?

我知道-Dmaven.test.skip=true,但这会关闭所有单元测试。
我也知道如何跳过集成测试,描述了here。但是我没有集成测试,只有单元测试,并且我没有任何对maven-surefire-plugin的显式调用。(我使用Maven 2和Eclipse-Maven插件)。

brtdzjyr

brtdzjyr1#

只跳过本模块中的测试怎么样?
在此模块的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>

最后,您可以创建一个配置文件来禁用测试(仍然是模块的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>

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

mfuanj7w

mfuanj7w2#

我认为这更简单,而且还具有为非万无一失的测试工作的好处(在我的例子中,是FlexUnitTests)

<profile>
   <id>noTest</id>
    <properties>
       <maven.test.skip>true</maven.test.skip>
    </properties>
 </profile>
eqzww0vc

eqzww0vc3#

如果您有一个大型的多模块项目,并且希望仅跳过某些模块中的测试,而无需使用自定义配置和分析更改每个模块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>

由于有了build-helper-maven-plugin,您实际上可以在构建过程中通过project.artifactId属性动态检查您是否在某个模块中(在构建过程中指向每个artifactId模块),然后正则表达式将寻找某些值的匹配(您想要跳过测试的模块名称)并相应地填充maven.test.skip属性(将其设置为true)。
在这种情况下,module1module3的测试将被跳过,而module2的测试将正常运行,即由正则表达式表示。
这种方法的优点是使其动态和集中化(在父pom.xml中),因此更便于维护:你可以在任何时候简单地通过改变上面的简单正则表达式来添加或删除模块。
显然,如果这不是构建的默认行为(推荐的情况),您可以始终将上面的代码片段 Package 在maven profile中。
您还可以更进一步,根据您的输入获得动态行为:

<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>

在这里,我们实际上用一个属性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)

也就是说,然后在运行时您决定,而不需要更改pom.xml文件或激活任何配置文件。

v1l68za4

v1l68za44#

使用Surefire Plugin 2.19,您可以简单地使用正则表达式排除您不想要的测试:
mvn '-Dtest=!%regex[.*excludedString.*]' test
上面的命令将排除所有包含 excludedString 的测试。

NB1如果使用双引号(“)而不是撇号('),则命令将无法正确解释,并将产生意外结果。(使用bash 3.2.57测试)
NB2需要特别注意使用多个版本的surefire插件的项目,2.19以上版本的surefire不支持正则表达式,不会执行任何测试。

版本管理(最好在父pom文件中添加此内容):

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

跳过测试的生成命令示例:https://artbcode.wordpress.com/2016/11/28/how-to-skip-a-subset-of-the-unit-tests/

46scxncf

46scxncf5#

我有一个与这个问题稍微不同的需求,这可能会有所帮助。我想从命令行中排除来自不同包的一些不同测试,所以单个通配符无法做到这一点。
我在Maven Failsafe文档中发现了排除规则,您可以指定一个逗号分隔的正则表达式或通配符排除列表:https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html
所以我的pomfile看起来像这样:

<excludes>
    <exclude>${exclude.slow.tests}</exclude>
</excludes>

我的命令行包含了以下内容:

mvn install "-Dexclude.slow.tests=**/SlowTest1.java, **/package/ofslowtests/*.java, **/OtherSlowTest.java"

对我来说,关键因素是在一个exclude语句中对一个maven属性进行一系列测试。

ig9co6j1

ig9co6j16#

对于maven-surefire-plugin,我建议使用一种稍微不同的方法来处理环境变量或属性。
在我的例子中,我需要使用参数testSourceDirectory。但是这个参数不被配置文件支持,所以我不能使用它们。使用配置文件意味着太多的新代码行。它使pom.xml更加复杂。

环境变量:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skipTests>${env.SKIP_UNIT_TESTS}</skipTests>
    </configuration>
</plugin>

如果需要跳过模块的测试,请将其设置为true。如果未设置,参数skipTests将被忽略,因此测试将运行。

属性:

<properties>
    <skipUnitTests>false</skipUnitTests>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>${skipUnitTests}</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

要跳过测试,请使用-DskipUnitTests=true运行mvn

相关问题