|
單元測(cè)試工具Junit是一個(gè)開源項(xiàng)目
昨天學(xué)習(xí)了一下這個(gè)東西,總結(jié)下心得
1.創(chuàng)建相應(yīng)的test類

package:測(cè)試類存放位置
Name:測(cè)試類名字
setUp,tearDown:測(cè)試類創(chuàng)建測(cè)試環(huán)境以及銷毀測(cè)試環(huán)境,這兩個(gè)方法只執(zhí)行一次
Class Under test:需要被測(cè)試的類路徑及名稱
點(diǎn)擊下一步就會(huì)讓你選擇需要給哪些方法進(jìn)行測(cè)試。
測(cè)試類創(chuàng)建完成后在類中會(huì)出現(xiàn)你選擇的方法的測(cè)試方法:
 1 package test.com.boco.bomc.alarmrelevance.show.dao; 2 3 import junit.framework.TestCase; 4 import org.junit.After; 5 import org.junit.Before; 6 import org.junit.BeforeClass; 7 import org.junit.Test; 8 9 public class ShowStrategyDaoTest extends TestCase{ 10 11 @BeforeClass 12 public static void setUpBeforeClass() throws Exception { 13 System.out.println("OK1"); 14 } 15 16 @Before 17 public void setUp() throws Exception { 18 } 19 20 @After 21 public void tearDown() throws Exception { 22 } 23 24 @Test 25 public final void testGetDataByApplyNameOrHostIp() { 26 fail("Not yet implemented"); // TODO 27 } 28 29 @Test 30 public final void testGetDataByObject() { 31 fail("Not yet implemented"); // TODO 32 } 33 34 @Test(timeout=1) 35 public final void testGetApplyUser() { 36 fail("Not yet implemented"); // TODO 37 } 38 39 @Test 40 public final void testGetVoiceUser() { 41 fail("Not yet implemented"); // TODO 42 } 43 44 @Test 45 public final void testSearchInAera() { 46 fail("Not yet implemented"); // TODO 47 } 48 49 @Test 50 public final void testGetDataByPolicyId() { 51 fail("Not yet implemented"); // TODO 52 } 53 54 }

其中的@before,@test,@after表示在執(zhí)行測(cè)試方法前執(zhí)行,需執(zhí)行的測(cè)試方法,在測(cè)試方法執(zhí)行后執(zhí)行
可以給@test添加timeout,exception參數(shù)
在測(cè)試方法中可以用assertEquals(arg0,arg1);
可以用TestSuite把多個(gè)測(cè)試類集中到一起,統(tǒng)一執(zhí)行測(cè)試,例如:
1 package test.com.boco.bomc.alarmrelevance.show.dao; 2 3 import junit.framework.Test; 4 import junit.framework.TestSuite; 5 6 7 public class TestAll { 8 public static Test suite(){ 9 TestSuite suite = new TestSuite("Running all the tests"); 10 suite.addTestSuite(ShowStrategyDaoTest.class); 11 suite.addTestSuite(com.boco.bomc.alarmrelevance.show.dao.ShowStrategyDaoTest.class); 12 return suite; 13 } 14 }
另外還可以把多個(gè)TestSuite組合到一個(gè)Test類里面,例如:
1 package test.com.boco.bomc.alarmrelevance.show.dao; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 7 public class TestAll1 extends TestCase { 8 public static Test suite(){ 9 TestSuite suite1 = new TestSuite("TestAll1"); 10 suite1.addTest(TestAll.suite()); 11 suite1.addTest(TestAll2.suite()); 12 return suite1; 13 } 14 }
這就更方便與集中測(cè)試,一個(gè)方法測(cè)試完了,可以對(duì)個(gè)方法,多個(gè)類一起測(cè)試
注意:在寫代碼的時(shí)候TestSuite,TestCase,Test的包不要到錯(cuò)了
測(cè)試效果如下:

----->froest
My cnblogs is :
http://www.cnblogs.com/God-froest/
Welcome you to exchange technology with me.
《夜的鋼琴曲八》
---石進(jìn)
Piano music can develop the temperament of a man.
|