您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
Junit 的使用經(jīng)驗(yàn)總結(jié)
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/1/5 14:23:08 ] 推薦標(biāo)簽:

經(jīng)驗(yàn)一、不要在測(cè)試用例的構(gòu)造函數(shù)中做初始化
當(dāng)我們需要增加一個(gè)測(cè)試時(shí),我們要書寫一個(gè)自己的測(cè)試用例,比如sometest。如果你喜歡在sometest的
構(gòu)造函數(shù)中做有關(guān)的初始化工作,這可不是個(gè)好習(xí)慣。如下例:
public class sometest extends testcase{
public sometest(string testname){
super(testname);
//初始化代碼
}
}
一旦初始化代碼產(chǎn)生異常,比如illegalstateexception,junit隨之將產(chǎn)生一個(gè)assertionfailederror,
并顯示類似下面的出錯(cuò)信息:
junit . framework . assertionfailederror : cannotinstantiatetestcase : test1at
junit . framework . assert . fail ( assert . java : 143 ) at
junit . framework . testsuite$1 . runtest ( testsuite . java : 178 ) at
junit . framework . testcase . runbare ( testcase . java : 129 ) at
junit . framework . testresult$1 . protect ( testresult .java : 100 ) at
junit . framework . testresult . runprotected ( testresult. java: 117 ) at
junit . framework . testresult . run ( testresult. java : 103 ) at
junit . framework . testcase . run( testcase . java: 120 ) at
junit . framework . testsuite . run( testsuite . java , compiledcode ) at
junit . ui . testrunner$12 . run (testrunner. java : 429 )
這一大堆出錯(cuò)信息只會(huì)讓人一頭霧水,我們只知道junit無(wú)法實(shí)例化某個(gè)測(cè)試用例,到底出了什么問(wèn)題,在
哪兒出錯(cuò)了呢?不知道!
那么好的做法是怎樣呢?
答案是重載測(cè)試用例的setup()方法進(jìn)行初始化。當(dāng)setup()中的初始化代碼產(chǎn)生異常時(shí)我們得到的
是類似下面的出錯(cuò)信息:
java . lang . illegalstateexception : oopsatbp . dtc . setup ( dtc .java: 34 ) at
junit . framework  . testcase . runbare ( testcase .java: 127 ) at
junit . framework  . testresult$ 1 . protect(testresult . java : 100 ) at
junit . framework  . testresult . runprotected ( testresult . java: 117 ) at
junit . framework  . testresult . run ( testresult .java : 103 )
...
顯然這要清楚得多我們一下子可以知道是在dtc.java 的第34 行產(chǎn)生了illegalstateexception

經(jīng)驗(yàn)二、不要假定測(cè)試用例中測(cè)試的執(zhí)行次序
我們知道在一個(gè)junit 的測(cè)試用例類中可以包含多個(gè)測(cè)試,每個(gè)測(cè)試其實(shí)是一個(gè)method。在下面的例子
中有兩個(gè)不同的測(cè)試,盡管testdothisfirst()在位置上先于testdothissecond(),但我們不能此假定
testdothisfirst()會(huì)先執(zhí)行。
public class sometestcase extends testcase{
public sometestcase(string testname){
super(testname);
}
public void testdothisfirst(){
...
}
public void testdothissecond(){
}
}
由于junit 內(nèi)部使用一個(gè)vector 來(lái)存儲(chǔ)所有的test,因此在不同的操作系統(tǒng)和java 虛擬機(jī)上,test 的執(zhí)行
次序是不可預(yù)測(cè)的。
好的習(xí)慣是保持測(cè)試之間的獨(dú)立性,使得它們?cè)谌魏未涡蛳聢?zhí)行的結(jié)果都是相同的。如果真得需要某些測(cè)試
按照特定的次序執(zhí)行,我們可以借助addtest 來(lái)實(shí)現(xiàn)。如下例:
public static testsuite(){
suite.addtest(new sometestcase(“testdothisfirst”;));
suite.addtest(new sometestcase(“testdothissecond”;));
return suite;
}
這樣我們可以確保junit先執(zhí)行testdothisfirst(),然后執(zhí)行testdothissecond()。

經(jīng)驗(yàn)三、測(cè)試要避免人工干預(yù)
如果某段測(cè)試代碼需要人工干預(yù),那至少有兩個(gè)不良后果:一則不能被包括在自動(dòng)測(cè)試中,比如夜間的回
歸測(cè)試;二則不能被重復(fù)執(zhí)行,例如數(shù)據(jù)刪除的測(cè)試不能做完刪除萬(wàn)事大吉,比較好的做法是自動(dòng)補(bǔ)上
刪除掉的數(shù)據(jù)。經(jīng)驗(yàn)二講的是不同的測(cè)試要避免相關(guān)性,而經(jīng)驗(yàn)三講的其實(shí)是測(cè)試要避免自相關(guān)。
經(jīng)驗(yàn)四、在子類中調(diào)用父類的setup() 和teardown()讓我們看一看下面的代碼
public class sometestcase extends anothertestcase {
// a connection to a database
private database thedatabase;
public sometestcase (string testname) {
super (testname);
}
public void testfeaturex () {
...
}
public void setup () {
// clear out the database
thedatabase.clear ();
}
}
你發(fā)現(xiàn)其中的錯(cuò)誤了嗎?setup()應(yīng)該調(diào)用super.setup() 以確保anothertestcase 中定義的父類的環(huán)境被初
始化了。當(dāng)然這也有例外,是基類可以處理任意的測(cè)試數(shù)據(jù)。

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd