跳过 Maven 中某些模块的测试

发布于 2024-07-17 06:22:24 字数 401 浏览 9 评论 0原文

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

问题:我该怎么做?

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

我还知道如何跳过集成测试,如此处所述。 但我没有集成测试,只有单元测试,并且没有对 maven-surefire-plugin 进行任何显式调用。 (我使用 Maven 2 和 Eclipse-Maven 插件)。

I would like my Maven builds to run most unit tests. But there are unit tests in one project which are slower and I'd like to generally exclude them; and occasionally turn them on.

Question: How do I do this?

I know about -Dmaven.test.skip=true, but that turns off all unit tests.

I also know about skipping integration tests, described here. But I do not have integration tests, just unit tests, and I don't have any explicit calls to the maven-surefire-plugin. (I am using Maven 2 with the Eclipse-Maven plugin).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悟红尘 2024-07-24 06:22:24

仅跳过此模块中的测试怎么样?

在此模块的 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,它将不会运行该模块的测试。

What about skipping tests only in this module ?

In the pom.xml of this module:

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

Eventually, you can create a profile that will disable the tests (still the pom.xml of the module) :

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

With the latter solution, if you run mvn clean package, it will run all tests. If you run mvn clean package -DnoTest=true, it will not run the tests for this module.

虫児飞 2024-07-24 06:22:24

我认为这更容易,并且还有进行非万无一失的测试的好处(在我的例子中,FlexUnitTests)

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

I think this is easier, and also has the benefit of working for non-surefire tests (in my case, FlexUnitTests)

<profile>
   <id>noTest</id>
    <properties>
       <maven.test.skip>true</maven.test.skip>
    </properties>
 </profile>
耳根太软 2024-07-24 06:22:24

如果您有一个大型多模块项目,并且只想跳过某些模块中的测试,而不需要使用自定义配置和分析来更改每个模块 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 中),因此更易于维护:您可以随时添加或删除模块,只需更改上面的简单正则表达式即可。

显然,如果这不是构建的默认行为(推荐情况),您始终可以将上面的代码片段包装在 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>

这里我们实际上用属性 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 文件或激活任何配置文件。

If you have a large multi-module project and you would like to skip tests only in certain modules without the need to change each of the module pom.xml file with custom configuration and profiling, you could add the following to the parent pom.xml file:

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

Thanks to the build-helper-maven-plugin you would actually dynamically check whether you are in a certain module or not during the build, via the project.artifactId property (pointing at each artifactId module during the build), the regex would then seek matching for certain values (the module names for which you want to skip tests) and populated the maven.test.skip property accordingly (setting it to true).

In this case, tests will be skipped for module1 and module3 while running properly for module2, that is, as expressed by the regex.

The advantage of this approach is to have it dynamic and centralized (in the parent pom.xml) hence better for maintenance: you could add or remove modules at any time simply by changing the simple regex above.

Obviously, if this is not the default behavior of the build (recommended case), you could always wrap the snippet above in a maven profile.


You could also go further and have dynamic behavior based on your input:

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

Here we are actually replacing the regex value with a property, test.regex, with default value to none (or whatever would not match any module name or, also, the default skipping matchings required).

Then from command line we could have

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)

That is, then at runtime you decide, without any need to change the pom.xml file or activating any profile.

悲喜皆因你 2024-07-24 06:22:24

使用 Surefire Plugin 2.19,您可以简单地使用正则表达式排除不需要的测试:

mvn '-Dtest=!%regex[.*excludedString.*]' test

上述命令将排除所有包含 excludedString 的测试。

NB1 如果使用双引号(")而不是撇号('),命令将无法正确解释,并会产生意外的结果。(使用 bash 3.2.57 测试)

NB2

(添加可能是一个好主意) 。这在父 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/

Using Surefire Plugin 2.19 you can simply exclude the tests you don't want using regular expressions:

mvn '-Dtest=!%regex[.*excludedString.*]' test

The above command will exclude all the tests that contain excludedString.

NB1 If double quotation mark(") is used instead of apostrophe(') the command will not be interpreted properly and will produce unexpected results. (Tested using bash 3.2.57)

NB2 Particular attention should be paid to projects in which multiple version of the surefire plugin is used. Versions of surefire older than 2.19 will not execute any tests because they do not support regular expressions.

Version management(it might be a good idea to add this in the parent pom file):

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

Examples of build commands that skip tests: https://artbcode.wordpress.com/2016/11/28/how-to-skip-a-subset-of-the-unit-tests/

┾廆蒐ゝ 2024-07-24 06:22:24

我对这个问题的需求略有不同,这可能会有所帮助。 我想从命令行中排除来自不同包的一些不同测试,因此单个通配符无法做到这一点。

我在排除的 Maven Failsafe 文档规则中发现,您可以指定正则表达式或通配符排除的逗号分隔列表:
https://maven.apache.org/surefire/ maven-failsafe-plugin/examples/inclusion-exclusion.html

所以我的pom文件看起来像这样:

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

我的命令行包括这个:

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

对我来说,关键要素是在单个排除中将一堆测试放入一个maven属性中陈述。

I had a slightly different need from this question that may prove helpful. I wanted to exclude from the command line a few different tests from different packages, so a single wildcard would not do it.

I found in the Maven Failsafe documentation rules for exclusions that you can specify a comma-separated list of either regex or wildcard exclusions:
https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

So my pomfile looked like this:

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

and my command line included this:

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

For me the key ingredient was getting a bunch of tests into one maven property in a single exclude statement.

秋叶绚丽 2024-07-24 06:22:24

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

For maven-surefire-plugin I would suggest a bit different approach with environment variables or properties.

In my case I need to use parameter testSourceDirectory. But this parameter is not supported by profiles, so I cannot use them. And using profiles means too many new lines of code. It makes pom.xml more complicated.

Environment variable:

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

Set it to true if you need to skip tests for the module. If it's not set, parameter skipTests will be ignored, so tests will run.

Properties:

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

To skip tests, run mvn with -DskipUnitTests=true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文