java - Maven - 不要编译测试类中的特定测试方法

标签 java maven

您可以使用以下命令跳过通过命令行执行测试:

mvn install -DskipTests

您还可以跳过执行特定单元测试,如下所示:

mvn -Dtest=\!com.example.ExampleTest#testName install

我知道您还可以使用 maven.test.skip 属性来跳过编译测试

mvn install -Dmaven.test.skip=true

有没有办法从命令行跳过特定单元测试的编译?换句话说,我不希望将特定测试包含在目标文件夹中(当然,不删除测试代码)。

最佳答案

要从 Maven 编译中排除测试类,您可以在 maven-compiler-plugin 声明中添加 testExclude

例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
      <!-- use testExcludes to exclude a class from the src/test tree -->
      <testExcludes>
        <testExclude>/path/to/your/class</testExclude>
      </testExcludes>
    </configuration>
</plugin>

更新基于此:

Kote> I'm looking to exclude a single/multiple unit test, not the whole class

glytching> You want the class to be compiled and run by Surefire but you want the compiled form to exclude an individual test (i.e. a method) within that class?

Kote> Yes

你可以...

  • 使用 -DskipTests 告诉 Maven 不要执行任何测试。
  • 使用 -Dtest=\!com.example.ExampleTest 告诉 Maven 不要执行特定的测试用例。
  • 使用 -Dtest=\!com.example.ExampleTest#testName 告诉 Maven 不要在特定测试用例中执行特定测试。
  • 使用 -Dmaven.test.skip 告诉 Maven 不要编译测试树中的任何类。
  • 使用 maven-compile-plugin::testExcludes 告诉 Maven 不要编译测试树中的特定类。

但是你不能告诉 Maven 不要编译类中的特定方法

听起来你需要一个 conditional compilation solution 。或者也许您应该在编译之前编辑该类。无论哪种方式,Maven(或 Java)都没有对此的内置支持。

关于java - Maven - 不要编译测试类中的特定测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49367591/

相关文章:

java - Apache Flink连接elasticsearch的问题

maven - 如何将 "--add-exports"编译器指令添加到 Maven 编译中

java - 删除项目的 Maven 性质后无法删除目标目录

Java 和 Bash shell

java - 无法返回对象类型?

java - 客户端/服务器只读取一条消息?

java - Maven java.exec环境变量被maven拾取,但未被类拾取

java - 使用 JNI 保护 Java 源代码

java - Win64-JNI : UnsatisfiedLinkError: Can't find dependent libraries

java - 多模块Maven项目中java编译失败: "cannot find symbol" when applying module-info.?