Junit 4 Tutorials(Junit 4 教程) 四、Junit4 參數化測試

來源:互聯網
上載者:User

標籤:junit4 測試 教程   教程   單元測試   eclipse   

Junit 4 參數化測試 允許通過變化範圍的參數值來測試方法。參數擦測試可以通過以下簡單的步驟實現:
  1. 對測試類別添加註解 @RunWith(Parameterized.class)
  2. 將需要使用變化範圍參數值測試的參數定義為私人變數
  3. 使用上一步驟聲明的私人變數作為入參,建立建構函式
  4. .建立一個使用@Parameters註解的公用靜態方法,它將需要測試的各種變數值通過集合的形式返回。
  5. 使用定義的私人變數定義測試方法

Junit 4 參數化測試範例

EvenNumberChecker.java 校正輸入的數字是否為偶數:

package in.co.javatutorials; /*** @author javatutorials.co.in*/public class EvenNumberChecker {     /**     * Is input number even.     *     * @param i input number     * @return <code>true</code> if input is even number; otherwise return false     */    public boolean isEven(int i) {        if (i % 2 == 0) {            return true;        } else {            return false;        }    }}

EvenNumberCheckerTest.java 對 EvenNumberChecker.java 進行參數化測試:

package in.co.javatutorials; import static org.junit.Assert.*; import java.util.Arrays;import java.util.Collection; import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters; /*** @author javatutorials.co.in*/// Step 1@RunWith(Parameterized.class)public class EvenNumberCheckerTest {     // Step 2: variables to be used in test method of Step 5    private int inputNumber;    private boolean isEven;     // Step 3: parameterized constructor    public EvenNumberCheckerTest(int inputNumber, boolean isEven) {        super();        this.inputNumber = inputNumber;        this.isEven = isEven;    }     // Step 4: data set of variable values    @Parameters    public static Collection<Object[]> data() {        Object[][] data = new Object[][] {                { 2, true },                { 5, false },                { 10, false }        };        return Arrays.asList(data);    }     @Test    public void test() {        System.out.println("inputNumber: " + inputNumber + "; isEven: " + isEven);        EvenNumberChecker evenNumberChecker = new EvenNumberChecker();        // Step 5: use variables in test code        boolean actualResult = evenNumberChecker.isEven(inputNumber);        assertEquals(isEven, actualResult);    }}

 範例輸出

       在Eclipse junit 視窗的輸出為:

      


      範例日誌輸出

inputNumber: 2; isEven: trueinputNumber: 5; isEven: falseinputNumber: 10; isEven: false


源碼下載

點擊我下載源碼

教程目錄導航
  • Junit測試架構介紹
  • Junit Eclipse教程
  • Junit 4註解
  • Junit 4斷言方法(Assert methods)
  • Junit 4參數化測試
  • Junit 4測試套件(Test Suite)
  • Junit 4忽略測試(Ignore Test)
  • Junit 4逾時測試(Timeout Test


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

本文出處為 http://blog.csdn.net/luanlouis,轉載請註明出處,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.