您可以使用以下命令跳过通过命令行执行测试:
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/