日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
單元測試?yán)鱆Unit的實踐與總結(jié)

單元測試工具Junit是一個開源項目,昨天學(xué)習(xí)了一下這個東西,總結(jié)下心得。

成都創(chuàng)新互聯(lián)是一家專業(yè)提供吉隆企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、成都做網(wǎng)站、HTML5建站、小程序制作等業(yè)務(wù)。10年已為吉隆眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。

1.創(chuàng)建相應(yīng)的test類

package:測試類存放位置。

Name:測試類名字。

setUp,tearDown:測試類創(chuàng)建測試環(huán)境以及銷毀測試環(huán)境,這兩個方法只執(zhí)行一次。

Class Under test:需要被測試的類路徑及名稱。

點擊下一步就會讓你選擇需要給哪些方法進(jìn)行測試。

測試類創(chuàng)建完成后在類中會出現(xiàn)你選擇的方法的測試方法:

 
 
 
 
  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.     } 

其中的@before,@test,@after表示在執(zhí)行測試方法前執(zhí)行,需執(zhí)行的測試方法,在測試方法執(zhí)行后執(zhí)行。

可以給@test添加timeout,exception參數(shù)。

在測試方法中可以用assertEquals(arg0,arg1);

可以用TestSuite把多個測試類集中到一起,統(tǒng)一執(zhí)行測試,例如:

 
 
 
 
  1. package test.com.boco.bomc.alarmrelevance.show.dao; 
  2.  
  3. import junit.framework.Test; 
  4. import junit.framework.TestSuite; 
  5.  
  6. public class TestAll { 
  7.     public static Test suite(){ 
  8.         TestSuite suite = new TestSuite("Running all the tests"); 
  9.         suite.addTestSuite(ShowStrategyDaoTest.class); 
  10.         suite.addTestSuite(com.boco.bomc.alarmrelevance.show.dao.ShowStrategyDaoTest.class); 
  11.         return suite; 
  12.     } 

另外還可以把多個TestSuite組合到一個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.     } 

這就更方便與集中測試,一個方法測試完了,可以對個方法,多個類一起測試。

注意:在寫代碼的時候TestSuite,TestCase,Test的包不要到錯了。

測試效果如下:

原文鏈接:http://www.cnblogs.com/God-froest/archive/2011/11/18/JunitTest.html

編輯推薦:

  1. Struts2單元測試:使用Junit測試Action
  2. Jython開發(fā)的JUnit測試包
  3. JUnit常用斷言方法
  4. 在Junit中測試私有函數(shù)的方法
  5. JUnit與JTiger的比較

本文標(biāo)題:單元測試?yán)鱆Unit的實踐與總結(jié)
文章轉(zhuǎn)載:http://www.5511xx.com/article/dhedihp.html