跳过Maven中某些模块中的测试
我希望我的Maven构建能够运行大多数单元测试。但是在一个项目中有一些单元测试速度较慢,我一般不考虑它们;偶尔打开它们 问题:我该怎么做 我知道跳过Maven中某些模块中的测试,maven,unit-testing,junit,maven-2,build-process,Maven,Unit Testing,Junit,Maven 2,Build Process,我希望我的Maven构建能够运行大多数单元测试。但是在一个项目中有一些单元测试速度较慢,我一般不考虑它们;偶尔打开它们 问题:我该怎么做 我知道-Dmaven.test.skip=true,但这会关闭所有单元测试 我还知道跳过集成测试,如上所述。但我没有集成测试,只有单元测试,也没有对maven surefire插件的任何显式调用。(我正在将Maven 2与EclipseMaven插件一起使用)。是否只跳过此模块中的测试 在本模块的pom.xml中: <project> [...
-Dmaven.test.skip=true
,但这会关闭所有单元测试
我还知道跳过集成测试,如上所述。但我没有集成测试,只有单元测试,也没有对maven surefire插件的任何显式调用。(我正在将Maven 2与EclipseMaven插件一起使用)。是否只跳过此模块中的测试 在本模块的pom.xml中:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
[...]
org.apache.maven.plugins
maven surefire插件
2.4.2
真的
[...]
最终,您可以创建一个概要文件来禁用测试(仍然是模块的pom.xml):
[...]
记事本
记事本
真的
org.apache.maven.plugins
maven surefire插件
2.4.2
真的
[...]
对于后一种解决方案,如果运行
mvn clean package
,它将运行所有测试。如果运行mvn clean package-DnoTest=true
,它将不会运行此模块的测试。我认为这更容易,而且还可以用于非surefire测试(在我的例子中,是FlexUnitTests)
记事本
真的
如果您有一个大型多模块项目,并且只想跳过某些模块中的测试,而不需要使用自定义配置和分析更改每个模块pom.xml
文件,则可以将以下内容添加到父pom.xml
文件中:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>maven.test.skip</name>
<value>${project.artifactId}</value>
<regex>(module1)|(module3)</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
在这里,我们实际上是用属性test.regex
替换regex值,默认值为none
(或者任何与任何模块名称不匹配的内容,或者需要的默认跳过匹配)
然后从命令行我们就可以
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)
也就是说,在运行时,您可以决定,无需更改
pom.xml
文件或激活任何配置文件。使用Surefire Plugin 2.19,您只需使用正则表达式排除不需要的测试:
mvn'-Dtest=!%正则表达式[.*excludedString.]'测试
上述命令将排除包含excludedString的所有测试
NB1如果使用双引号(“)而不是撇号(”),则该命令将无法正确解释,并将产生意外结果。(使用bash 3.2.57测试)
NB2应特别注意使用多个版本的surefire插件的项目。早于2.19的surefire版本不会执行任何测试,因为它们不支持正则表达式
版本管理(最好将其添加到父pom文件中):
org.apache.maven.plugins
我的需求与这个问题稍有不同,这可能会有所帮助。我想从命令行中排除来自不同软件包的一些不同测试,因此一个通配符就不行了
我在Maven Failsafe文档排除规则中发现,您可以指定以逗号分隔的正则表达式或通配符排除列表:
所以我的文件看起来像这样:
<excludes>
<exclude>${exclude.slow.tests}</exclude>
</excludes>
对我来说,关键因素是在一个exclude语句中对一个maven属性进行一系列测试。感谢这一点。第一个代码段跳过了测试;我可能稍后会使用您的进一步建议定义另一个配置文件。我的困惑在于,我的pom隐式调用了surefire。没有提到urefire插件在我的pom.xml中。尽管如此,配置surefire插件的代码还是正确的。对于其他人来说,另一个选择是跳过整个模块,直到你想要构建和验证它:下面是如何创建maven概要文件,只有在它被激活的情况下才会运行测试:除了项目中的几个模块外,你如何对所有模块执行此操作?可以吗有人在这里向我解释一下noTest
的用途吗?这不会跳过这里的测试,不管有没有-DnoTest=trues这是因为你应该使用-PnoTest而不是-DnoTest=trueSetting maven.test.skip到true也有跳过测试编译的效果,因为skipTests仍然编译测试,但不运行它们。你知道怎么做吗跳过多个模式?-Dtests
似乎与surefire不兼容。以下原因导致跳过我的所有测试:mvn'-Dtest=!%regex[.*DeviceLogServiceTest.*]'--安静安装-Dsurefire.useFile=false-Dsurefire.printSummary=false-Dmaven.test.skip=false
<properties>
<test.regex>none</test.regex>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>maven.test.skip</name>
<value>${project.artifactId}</value>
<regex>${test.regex}</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
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)
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<excludes>
<exclude>${exclude.slow.tests}</exclude>
</excludes>
mvn install "-Dexclude.slow.tests=**/SlowTest1.java, **/package/ofslowtests/*.java, **/OtherSlowTest.java"