您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnit介紹及其快速使用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/19 15:03:46 ] 推薦標(biāo)簽:

  現(xiàn)在已經(jīng)了解了JUnit3.8.1的使用和其基本的工作原理,JUnit 4是JUnit框架有史以來的大改進(jìn),其主要目標(biāo)便是利用Java 5的Annotation特性簡化測試用例的編寫。下面同樣通過代碼來學(xué)習(xí)一下:

  1.右擊該類,選擇 新建->JUnit測試用例,選擇JUnit4,setUp和tearDown方法,點(diǎn)擊下一步,選擇需要測試的方法,JUnit會自動生成測試的代碼框架(可以看到這個(gè)代碼框架和上面的有較大的不同),手動添加自己的測試代碼后如下:

 
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class HelloTest4 {        //這里不需要繼承自TestCase

    private Hello hello;
    public HelloTest4()
    {
        super();
        System.out.println("a new test instance...");
    }
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("call before all tests...");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("call after all tests... ");
    }
    @Before
    public void setUp() throws Exception {
        //這里不需要調(diào)用super.setUp()
        System.out.println("call before test...");
        hello=new Hello();
    }

    @After
    public void tearDown() throws Exception {
        //這里不需要調(diào)用super.tearDown()
        System.out.println("call after test... ");
    }

    @Test
    public void testAbs() {
        System.out.println("test the method abs()");
        assertEquals(16, hello.abs(16));
        assertEquals(11, hello.abs(-10));//在這里,會出現(xiàn)故障,應(yīng)該把左邊的參數(shù)改為10
        assertEquals(0, hello.abs(0));
    }

    @Test
    public void testDivision() {
        System.out.println("test the method division()");
        assertEquals(3D, hello.division(6, 2));
        assertEquals(6D, hello.division(6, 1));
        assertEquals(0D, hello.division(6, 0));//在這里,會出現(xiàn)故障(與3.8.1有些不同?)
    }

    //下面,并不是對JunitDemo類中成員函數(shù)的測試,只是演示JUnit的一些功能
   
    //測試是否會發(fā)生期望的異常
    @Test(expected=ArithmeticException.class)
    public void testDiv0() {
        System.out.println("test the method Div0()");
        double result=100/0;
    }
   
    //測試是否超時(shí)
    @Test(timeout=1)
    public void testLongTimeTask()
    {
        System.out.println("test the method LongTimeTask()");
        double d = 0;
        for(int i=1; i<10000000; i++)
            d+=i;
    }
   
}

  2.運(yùn)行該測試類,輸出如下:

call before all tests...
a new test instance...
call before test...
test the method abs()
call after test...

a new test instance...
call before test...
test the method division()
call after test...

a new test instance...
call before test...
test the method Div0()
call after test...

a new test instance...
call before test...
test the method LongTimeTask()
call after test...

call after all tests...
  3.從上面的輸出結(jié)果可以看出,JUnit的工作原理和以前的幾乎還是沒有變的,只是讓用戶使用更簡單了當(dāng)然有一個(gè)變化是3.8.1的所有測試實(shí)例是測試前全都創(chuàng)建好的,而JUnit4的測試實(shí)例是在每個(gè)測試前創(chuàng)建的。

  4.當(dāng)JUnit4還有一個(gè)與以前很大的不同是引入了@BeforeClass和@AfterClass(可以在選擇setUp和tearDown的時(shí)候選擇setUpBeforeClass()和tearDownAfterClass()),setUpBeforeClass()在所有測試前調(diào)用,tearDownAfterClass()在所有測試后調(diào)用,它們不同與setUp和tearDown,在整個(gè)測試過程中只會被調(diào)用一次,這是為了能在@BeforeClass中初始化一些昂貴的資源,例如數(shù)據(jù)庫連接,然后執(zhí)行所有的測試方法,后在@AfterClass中釋放資源。

  總結(jié),這里只是對JUnit對class的單元測試作了簡單的討論,除此以外,JUnit還可以對JSP,Servlt,EJB等做單元測試。

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