單元測試框架之——JUnit

Java語言編寫的Webdriver測試程序通常使用單元測試框架運行,所以有必要了解單元測試框架的使用技巧。
單元測試框架包括JUnit單元測試框架和TestNG單元測試框架

JUnit單元測試框架
1、安裝JUnit4
步驟如下:
(1)新建一個Java工程
(2)右擊工程,選擇“properties”標籤欄,單擊“add library”按鈕。
(3)選擇“Java build path”選項,單擊“library”標籤欄,單擊“add library”按鈕。
(4)選擇“JUnit”選項,單擊“next”按鈕。
(5)在彈出的對話框中,點擊“finish”按鈕。
(6)在“Java build path”對話框中,顯示JUnit圖標,表示引入JUnit4成功。
2、JUnit的常見註解
被測試代碼如下:

package cn.gloryroad;

public class Calculator {

    public int result = 0;
    public int add(int operand1,int operadn2){
        result = operand1 + operadn2;//兩數相加
        return result;
    }
    public int subtract(int operand1,int operand2){
        result = operand1 - operand2;//兩數相減
        return result;
    }
    public int multiple(int operand1,int operand2){
        result = operand1 * operand2;//兩數相乘
        for(;;){

        }//此處加入一個死循環
    }
    public int divide(int operand1,int operand2){
        result = operand1 / 0;//除數爲零的除法運算
        return result;
    }
    public int getresult(){
        return this.result;//返回計算結果
    }

} 

創建JUnit4的測試代碼:
(1)右擊Calculator類所在的包,選擇“New”->”JUnit Test Case”命令。
(2)彈出”JUnit Test Case”的對話框,在“name”輸入框輸入“CalculatorTest”,並勾選“setUpBeforeClass()”、“tearDownAfterClass()”、“setUp()”、“tearDown()”四個選項,並在“Class under test”輸入框中輸入“cn.gloryroad.Calculator”,點“finish”完成。
(3)自動生成以下代碼。完成JUnit4測試用例的創建工作,基於基本的測試需求,可在此模板基礎上編寫單元測試代碼。

package cn.gloryroad;

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 CalculatorTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {
        fail("Not yet implemented");
    }

針對Calculator類的內部實現邏輯,創建如下測試代碼

package cn.gloryroad;

import static org.junit.Assert.*;

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

public class CalculatorTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("@BeforeClass");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("@AfterClass");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("測試開始");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("測試結束");
    }

    @Test
    public void testadd() {
        Calculator cal = new Calculator();
        cal.add(2, 2);
        assertEquals(4, cal.getresult());
    }

    @Test
    public void testSubstract(){
        Calculator cal = new Calculator();
        cal.subtract(4, 2);
        assertEquals(2, cal.getresult());
    }

    @Ignore
    public void testMultiple(){
        fail("Not yet implemented");
    }

    @Test(timeout = 2000)//表示此用例執行時間不能超過2秒
    public void testDivide(){
        for(;;);
    }

    @Test(expected = ArithmeticException.class)//判斷是否爲ArithmeticException異常,如果是,則設定此測試用例爲成功執行狀態。
    public void testDivideByZero(){
        Calculator cal = new Calculator();
        cal.divide(4, 0);
    }

}

運行結果:
這裏寫圖片描述

JUnit常見註解及其含義:
這裏寫圖片描述

3、創建JUnit4 Test Suit
步驟如下:
(1)在同一個包下新建一個測試類,命名爲“TestCalculator2”,測試類的具體代碼如下:

package cn.gloryroad;

import org.junit.Test;

public class TestCalculator2 {
    @Test
    public void test() {
        System.out.println("TestCalculator2的測試方法被調用");
    }

}

(2)創建成功後,右擊工程名稱,選擇“new”->“other”命令。
(3)在彈出的對話框中選擇“Java”->“JUnit”下的“JUnit Test Suit”選項,點擊“next”。
(4)在彈出的對話框中,選中“TestCalculator”和“TestCalculator2”這兩個類,點擊“finish”。
(5)自動生成了一個名爲“AllTests”的測試類,執行此類代碼,兩個測試類均被執行。
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章