【測試之體系,人人皆有一磚一瓦】
【概述】
測試是非常重要的,構建一個好的測試對於系統的質量、重構非常重要。下面簡單介紹下測試的一些框架,有的是帶過,此篇主要是我這次遷移測試框架的一些總結。如果大家對其中的一些框架比較感興趣,可以去研究下。 推薦:junit\testng\jmockit 。對於每個開發工程師肯定會大有益處的。其中會涉及到很到的java技術點。
【選型】
【遷移】
最近幾天在遷移單元測試,簡單看了下以前的單元測試框架。
<groupId>com.alibaba.external</groupId>
<artifactId>test.junit</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>sourceforge.spring.modules.test</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.testng.jdk15</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.unitils</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.dbunit</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.hamcrest-all</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.mockito-all</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.jmock</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.jmockit</artifactId>
<groupId>com.alibaba.external</groupId>
<artifactId>test.jmock-legacy</artifactId>
其中有很多的框架在項目中根本就沒有用到,所以就需要進行清理,所以我把在測試階段的類加載信息打印出來(-XX:TraceClassLoading),再分析。不過這個比較繁瑣
後來還是直接只用mvn dependency:analyze,來分析,
{這些Unused declared dependencies 列表並不準確,如果project中還有大量反射等,移除包會出現想不到的意外,此點需要慎重。}
分析後發現以下的框架在系統中沒有應用:
test.hamcrest-all.jar(1.2)
http://code.google.com/p/hamcrest/
Hamcrest it is not a testing library: it just happens that matchers are very useful for testing這個就不講了。
jmock jmock-legacy(2.5.1)
把以上的測試框架全部去掉。
後來進入,再加入了jtester測試框架
【兼容】
【工程中既有 testng又有junit的單元測試,怎麼都能跑呢?】
本質testng是支持 junit單元測試的。
在maven中單元測試是用surefire來跑的。
這個就需要在surefire來設定了。如果不設置suiteXmlFiles屬性,surefire會自動生成suiteXmlFile。他會自動生成testng文件,生成的規則是找test開頭或者結尾的類。
如果想指定,則需要junit與testng分別寫suite。其中一個junit屬性設置爲true。
【思考】
對於比較龐大的項目,那麼測試的易用性非常重要。jtester是非常易用,並且他是開源的。不過對於文檔和代碼,好像作者沒有及時更新啊。如果有朋友需要使用,可以聯繫我。嘿嘿。。。
如果項目比較小,建議還是使用原生的junit與testng。