天天看點

單元測試架構之——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 = ;
    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 / ;//除數為零的除法運算
        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(, );
        assertEquals(, cal.getresult());
    }

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

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

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

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

}
           

運作結果:

單元測試架構之——JUnit

JUnit常見注解及其含義:

單元測試架構之——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”的測試類,執行此類代碼,兩個測試類均被執行。

單元測試架構之——JUnit

繼續閱讀