在编写写Junit脚本后,进行全网回归的时候,肯定会接触Maven的surefire插件。
基本配置如下:
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <argLine>-Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m</argLine>
- <additionalClasspathElements>
- <additionalClasspathElement>
- ${basedir}/target/test-classes
- </additionalClasspathElement>
- </additionalClasspathElements>
- <includes>
- <include>**/*Test.java</include>
- </includes>
- <excludes>
- <exclude>**/TestConstants.java</exclude>
- </excludes>
- <forkMode>pertest</forkMode>
- </configuration>
- </plugin></span>
Maven运行测试用例时,是通过调用maven的surefire插件并fork一个子进程来执行用例的。forkmode属性中指明是要为每个测试创建一个进程,还是所有测试在同一个进程中完成。
forkMode 可设置值有 “never”, “once”, “always” 和 “pertest”。
pretest: 每一个测试创建一个新进程,为每个测试创建新的JVM是单独测试的最彻底方式,但也是最慢的,不适合hudson上持续回归。
once:在一个进程中进行所有测试。once为默认设置,在Hudson上持续回归时建议使用默认设置。
always:在一个进程中并行的运行脚本,Junit4.7以上版本才可以使用,surefire的版本要在2.6以上提供这个功能,其中 threadCount:执行时,指定可分配的线程数量。只和参数parallel配合使用有效。默认:5。
- <forkMode>always</forkMode>
- <parallel>methods</parallel>
- <threadCount>4</threadCount></span>
surefire里还有其它一些有趣的参数,如果有兴趣,你可以访问
http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html 来了解更多信息。