maven:如何通过命令行选项跳过某些项目中的测试?

发布于 2025-01-02 00:29:41 字数 351 浏览 5 评论 0原文

在我的 Maven 项目中,我有许多模块。是否可以通过命令行选项关闭某些模块的运行单元测试?

我的项目大约需要 15 分钟才能完成所有单元测试。我想通过仅运行我正在处理的模块中的单元测试来加快整体构建速度。我不想进入并编辑每个单独的 pom.xml 来实现此目的。

我已经尝试了此处概述的解决方案: 我可以运行通过 Maven 进行特定的 testng 测试组? 然而,结果是我想跳过的模块中存在大量测试失败。我想“组”不是模块的同一概念吗?

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?

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

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

发布评论

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

评论(4

终止放荡 2025-01-09 00:29:41

要打开和关闭整个项目的单元测试,请使用 Maven Surefire Plugin 具有跳过测试的功能。从命令行使用skipTests 有一个缺点。在多模块构建场景中,这将禁用所有模块的所有测试。

如果您需要对运行模块的测试子集进行更精细的控制,请考虑使用 Maven Surefire 插件的测试包含和排除功能

要允许命令行覆盖,请在配置 Surefire 插件时使用 POM 属性。以下面的 POM 段为例:

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

使用上面的 POM,您可以通过多种方式执行测试。

  1. 运行所有测试(以上配置包括所有 **/*Test.java 测试源文件)
mvn 测试
  1. 跳过所有模块的所有测试
mvn -DskipTests=true 测试
  1. 跳过特定模块的所有测试
mvn -DsomeModule.skip.tests=true 测试
  1. 仅对特定模块运行某些测试(此示例包括所有 **/*IncludeTest.java 测试源文件)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" 测试
  1. 排除特定模块的某些测试(此示例排除所有 **/*ExcludeTest.java 源文件)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" 测试

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
∞梦里开花 2025-01-09 00:29:41

找到了一种在命令行上排除的方法:

# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method 
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*

这来自:使用 Maven 运行一项或排除一项测试

Found a way to exclude on command line:

# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method 
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*

This is from: Run one or Exclude one test with Maven

扎心 2025-01-09 00:29:41

...如果你想将参数传递给 Hudson/Jenkins 中的 Maven 发布插件,你必须使用
-Darguments=-DskipTests
让它发挥作用。

…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use
-Darguments=-DskipTests
to get it work.

记忆之渊 2025-01-09 00:29:41

如果您想使用 Maven 配置文件:

你可能想让它像这样工作:

不知道有没有支持的命令行选项具有相同的功能。

您也可以尝试直接使用环境属性,如本文档页面所示:

即类似:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
    <configuration>
      <skipTests>${moduleA.skipTests}</skipTests>
    </configuration>
  </plugin>

然后使用 mvn -DmoduleA.skipTests=false test 测试该模块。

If you want to use Maven profiles:

you might want to make it work doing something like this:

I don't know if there is a supported command line option that does the same.

You also might try using environment properties directly, something as per this doc page:

i.e. something like:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
    <configuration>
      <skipTests>${moduleA.skipTests}</skipTests>
    </configuration>
  </plugin>

then using mvn -DmoduleA.skipTests=false test to test that one module.

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