在編寫寫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>
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>
surefire裏還有其它一些有趣的參數,如果有興趣,你可以訪問
http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html 來了解更多信息。