【问题标题】:How to permanently exclude one test class in a Maven build如何在 Maven 构建中永久排除一个测试类
【发布时间】:2014-10-06 01:05:24
【问题描述】:

我正在尝试从我的 Maven 构建中排除单个测试(我不希望编译或执行该测试)。以下不起作用:

<project ...>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/MyTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

实现目标的正确方法是什么?我知道我可以使用命令行选项-Dmaven.test.skip=true,但我希望它成为pom.xml 的一部分。

【问题讨论】:

  • 首先:你不应该这样做,测试是要运行的。第二:这类问题很容易通过google或在SO中搜索找到。
  • @Second:我确实搜索了很长时间,我也尝试了一些应该解决问题的方法(不成功)。如果您找到了描述如何解决此问题的资源,请与我分享。
  • 如果你必须使用 bit.ly 来绕过 url 禁令,你认为你的思维方式会受到 stackoverflow 社区的欢迎吗?
  • 网址禁止?什么?你的问题解决了吗?从您上次的编辑中可以看出您知道maven.test.skip 选项。那么真正的问题是什么?
  • a,b) 我没试过,但上次我检查了 lmgtfy 网址被禁止meta.stackexchange.com/a/34456/160289。 c) 不,d) 我正在尝试在 pom 文件中从我的测试套件中排除 single 测试。

标签: maven maven-surefire-plugin maven-compiler-plugin


【解决方案1】:

使用解释标记 (!) 排除一个测试类

mvn test -Dtest=!LegacyTest

排除一种测试方法

mvn verify -Dtest=!LegacyTest#testFoo

排除两种测试方法

mvn verify -Dtest=!LegacyTest#testFoo+testBar

使用通配符 (*) 排除包

mvn test -Dtest=!com.mycompany.app.Legacy*

来自:https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/

【讨论】:

    【解决方案2】:

    跳过测试

    来自docs,如果你想跳过一个测试,你可以使用:

    <project>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
              <excludes>
                <exclude>**/MyTest.java</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    看看区别,在你的例子中,你使用&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;,而文档说你应该使用&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;插件。

    而且,如果你想禁用所有测试,你可以使用:

        <configuration>
          <skipTests>true</skipTests>
        </configuration>
    

    另外,如果您使用的是JUnit,您可以使用@Ignore,并添加一条消息。

    从编译中排除测试

    来自this的回答,可以使用。诀窍是拦截<id>default-testCompile</id> <phase>test-compile</phase>(默认测试编译阶段)并排除该类:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <id>default-testCompile</id>
          <phase>test-compile</phase>
          <configuration>
            <testExcludes>
              <exclude>**/MyTest.java</exclude>
            </testExcludes>
          </configuration> 
          <goals>
            <goal>testCompile</goal>
          </goals>
        </execution>                  
      </executions>
    </plugin>
    

    【讨论】:

      【解决方案3】:

      在 Maven 中默认跳过 test 的编译和执行的最简单方法是在 pom.xml 中添加以下属性:

       <properties>
          <maven.test.skip>true</maven.test.skip>
       </properties>
      

      您仍然可以通过从命令行覆盖属性来更改行为:

      -Dmaven.test.skip=false
      

      或者通过激活个人资料:

      <profiles>
          <profile>
              <id>testing-enabled</id>
              <properties>
                 <maven.test.skip>false</maven.test.skip>
              </properties>
          </profile>
      </profiles> 
      

      【讨论】:

      • 有没有办法只禁用一个测试而不是全部?
      猜你喜欢
      • 1970-01-01
      • 2013-01-20
      • 2012-08-20
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2011-02-05
      • 2012-05-27
      相关资源
      最近更新 更多