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)
6条答案
按热度按时间brtdzjyr1#
只跳过本模块中的测试怎么样?
在此模块的pom.xml中:
最后,您可以创建一个配置文件来禁用测试(仍然是模块的pom.xml):
对于后一种解决方案,如果您运行
mvn clean package
,它将运行所有测试。如果您运行mvn clean package -DnoTest=true
,它将不会运行此模块的测试。mfuanj7w2#
我认为这更简单,而且还具有为非万无一失的测试工作的好处(在我的例子中,是FlexUnitTests)
eqzww0vc3#
如果您有一个大型的多模块项目,并且希望仅跳过某些模块中的测试,而无需使用自定义配置和分析更改每个模块
pom.xml
文件,则可以将以下内容添加到父pom.xml
文件中:由于有了
build-helper-maven-plugin
,您实际上可以在构建过程中通过project.artifactId
属性动态检查您是否在某个模块中(在构建过程中指向每个artifactId
模块),然后正则表达式将寻找某些值的匹配(您想要跳过测试的模块名称)并相应地填充maven.test.skip
属性(将其设置为true
)。在这种情况下,
module1
和module3
的测试将被跳过,而module2
的测试将正常运行,即由正则表达式表示。这种方法的优点是使其动态和集中化(在父
pom.xml
中),因此更便于维护:你可以在任何时候简单地通过改变上面的简单正则表达式来添加或删除模块。显然,如果这不是构建的默认行为(推荐的情况),您可以始终将上面的代码片段 Package 在maven profile中。
您还可以更进一步,根据您的输入获得动态行为:
在这里,我们实际上用一个属性
test.regex
替换了正则表达式的值,默认值为none
(或者任何不匹配任何模块名称的值,或者默认跳过所需的匹配)。然后从命令行我们可以有
也就是说,然后在运行时您决定,而不需要更改
pom.xml
文件或激活任何配置文件。v1l68za44#
使用Surefire Plugin 2.19,您可以简单地使用正则表达式排除您不想要的测试:
mvn '-Dtest=!%regex[.*excludedString.*]' test
上面的命令将排除所有包含 excludedString 的测试。
NB1如果使用双引号(“)而不是撇号('),则命令将无法正确解释,并将产生意外结果。(使用bash 3.2.57测试)
NB2需要特别注意使用多个版本的surefire插件的项目,2.19以上版本的surefire不支持正则表达式,不会执行任何测试。
版本管理(最好在父pom文件中添加此内容):
跳过测试的生成命令示例:https://artbcode.wordpress.com/2016/11/28/how-to-skip-a-subset-of-the-unit-tests/
46scxncf5#
我有一个与这个问题稍微不同的需求,这可能会有所帮助。我想从命令行中排除来自不同包的一些不同测试,所以单个通配符无法做到这一点。
我在Maven Failsafe文档中发现了排除规则,您可以指定一个逗号分隔的正则表达式或通配符排除列表:https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html
所以我的pomfile看起来像这样:
我的命令行包含了以下内容:
对我来说,关键因素是在一个exclude语句中对一个maven属性进行一系列测试。
ig9co6j16#
对于maven-surefire-plugin,我建议使用一种稍微不同的方法来处理环境变量或属性。
在我的例子中,我需要使用参数
testSourceDirectory
。但是这个参数不被配置文件支持,所以我不能使用它们。使用配置文件意味着太多的新代码行。它使pom.xml更加复杂。环境变量:
如果需要跳过模块的测试,请将其设置为
true
。如果未设置,参数skipTests
将被忽略,因此测试将运行。属性:
要跳过测试,请使用
-DskipUnitTests=true
运行mvn