问题描述
- 如何让maven跳过某些指定的Test用例
-
maven打包的时候,会默认取执行Test用例,当然也可以忽略错误的,也可以直接跳过。
现在需求是这样的,能不能指定忽略某些测试用例?
比如我有一类测试用例,是用来测试Http接口的,这些接口如果测我本地的就是url就是localhost:8080/..../ 而这种我肯定是不想让maven来执行这些用例的,因为你要是本机没起服务,那这个测试用例就得等到timeOut才会执行完。一般都是运维得打包。那肯定这个url的服务是访问不到的
但又不是全部测试用例不执行,比如某些工具类的方法,我们也是有些测试用例的,这些测试用例肯定是需要保证能执行通过的。
所以,有没有什么办法,告诉maven,哪些Test用例不用执行,哪些需要执行。而不是全部忽略,或者全部执行
解决方案
A list of elements specifying the tests (by
pattern) that should be included in testing. When not
specified and when the test parameter is not specified
the default includes will be
/Test*.java
/*Test.java
**/*TestCase.java
maven-surefire-plugin 插件里面的includes标签 可以使用通配符指定特定的测试类. 当然excludes标签就是排除一些类了.
你这种情况比较简单了 不嫌麻烦就在excludes里面把所有不测试的类都写一遍. 不然就重命名使所有不测试的具有相同的规则比如XxxHttpTest 就可以使用通配符排除了
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>-Xmx256M</argLine>
<skip>${maven.test.skip}</skip>
<testFailureIgnore>${maven.test.skip}</testFailureIgnore>
<includes></includes>
<excludes></excludes>
</configuration>
</plugin>
解决方案三:
maven命令时可以携带虚拟机参数:-dmaven.test.skip=true。如果是eclipse执行maven命令,可以配置该命令:Run as->Run Configurations->勾上Skip Tests