亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnit介紹及其快速使用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 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方法,點擊下一步,選擇需要測試的方法,JUnit會自動生成測試的代碼框架(可以看到這個代碼框架和上面的有較大的不同),手動添加自己的測試代碼后如下:

 
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;
    }
   
    //測試是否超時
    @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.運行該測試類,輸出如下:

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)然有一個變化是3.8.1的所有測試實例是測試前全都創(chuàng)建好的,而JUnit4的測試實例是在每個測試前創(chuàng)建的。

  4.當(dāng)JUnit4還有一個與以前很大的不同是引入了@BeforeClass和@AfterClass(可以在選擇setUp和tearDown的時候選擇setUpBeforeClass()和tearDownAfterClass()),setUpBeforeClass()在所有測試前調(diào)用,tearDownAfterClass()在所有測試后調(diào)用,它們不同與setUp和tearDown,在整個測試過程中只會被調(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
主站蜘蛛池模板: 亚洲国产成人久久一区www | 激情欧美一区二区三区中文字幕 | 99艾草视频在线播放 | 星空影院免费观看韩国三集 | 首页 国产 制服 丝袜 | 成年美女黄网站色大免费观看软件 | 黄色三级视屏 | 亚洲伊人色一综合网 | 一区二区三区国产 | 色播五月激情五月 | 亚洲无线一二三四手机 | 日本一二三区在线视频 | 在线视频精品播放 | 欧美区在线播放 | 久久久久久久国产a∨ | 欧美色穴 | 天天综合天天添夜夜添狠狠添 | 亚洲精品无码不卡 | 免费在线一级毛片 | 一级片黄色a | 成年人视频在线播放 | 99国产精品久久久久久久成人热 | 伊人网网站 | 最新黄色免费网站 | 国产精品天天看特色大片不卡 | 日韩专区亚洲综合久久 | 国产精品视频在 | 香蕉视频免费网站 | 一级黄大片 | 天天澡天天摸天天添视频 | 亚洲小视频在线播放 | 两性午夜又粗又大又爽视频 | 亚洲图片欧美文学小说激情 | 国精品日韩欧美一区二区三区 | 亚洲第一二三四区 | 亚洲精品国产综合一线久久 | 亚洲网站在线观看 | 全日本爽视频在线 | 狠狠色欧美亚洲狠狠色五 | 日韩成人毛片高清视频免费看 | 丁香综合激情 |