电竞比分网-中国电竞赛事及体育赛事平台

分享

Junit使用

 看風(fēng)景D人 2014-01-14

單元測試工具Junit是一個開源項目

昨天學(xué)習(xí)了一下這個東西,總結(jié)下心得

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

package:測試類存放位置

Name:測試類名字

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

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

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

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

復(fù)制代碼
 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 }
復(fù)制代碼

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

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

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

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

復(fù)制代碼
 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 }
復(fù)制代碼

另外還可以把多個TestSuite組合到一個Test類里面,例如:

復(fù)制代碼
 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 }
復(fù)制代碼

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

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

測試效果如下:

                                       ----->froest

My cnblogs is :

http://www.cnblogs.com/God-froest/

Welcome you to exchange technology with me.

《夜的鋼琴曲八》

                              ---石進

Piano music can develop the temperament of a man.



 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多