天天看點

Junit 入門(一)

    Junit 簡介

JUnit是一個Java語言的單元測試架構。它由Kent Beck和Erich Gamma建立,逐漸成為源于Kent Beck的sUnit的xUnit家族中最為成功的一個。 JUnit有它自己的JUnit擴充生态圈。多數Java的開發環境都已經內建了JUnit作為單元測試的工具。 

JUnit是由 Erich Gamma 和 Kent Beck 編寫的一個回歸測試架構(regression testing framework)。Junit測試是程式員測試,即所謂白盒測試,因為程式員知道被測試的軟體如何(How)完成功能和完成什麼樣(What)的功能。Junit是一套架構,繼承TestCase類,就可以用Junit進行自動測試了。

    Junit 3.x 安裝使用

           1.從Junit.org 下載下傳Junit 3.x 版本,在第二步中稱為 junitx.x.x.zip.

           2.把junitx.x.x.zip 解壓到任何目錄。例如:D:\tools\junit。

           安裝完畢。

           打開一個指令視窗到D:\tools\junit。

           可以用如下指令run 一下junit 自帶的例子:

           java -cp junit.jar;. junit.swingui.TestRunner junit.samples.AllTests

           将會看到Juint 自帶的視窗UI(如下圖)。

Junit 入門(一)
Junit 入門(一)
Junit 入門(一)

    第一個Junit單元測試

      待測類:

public class Largest{
	
	public static int largest(int[] list){
		
		int index, max = Integer.MAX_VALUE;
		for(index=0; index < list.length-1; index++){
			if(list[index] > max){
				max = list[index];
			}
		}
		
		return max;
	}
}
           

         測試類:

import junit.framework.*;
public class TestLargest extends TestCase {
	public TestLargest(String name){
		super(name);
	}
	
	public void testSimple(){
		assertEquals(9, Largest.largest(new int[]{9,8,7}));
	}

}
           

        打開指令視窗到源檔案目錄,用如下指令編譯java 源檔案:         javac -cp "..\..\..\tools\junit\junit3.8.1\junit3.8.1\junit.jar" *.java

        編譯完成之後, 用如下指令運作測試用例:         java -cp .;"..\..\..\tools\junit\junit3.8.1\junit3.8.1\junit.jar" junit.swingui.TestRunner TestLargest

        Oh ho ho, 出師不利啊...         

Junit 入門(一)

       Junit 報告測試failed,檢查代碼,發現bug,修改代碼,重新測試。

public class Largest{
	
	public static int largest(int[] list){
		
		int index, max = 0;
		for(index=0; index < list.length-1; index++){
			if(list[index] > max){
				max = list[index];
			}
		}
		
		return max;
	}
}
           

        Passed. Ju bar is green.

Junit 入門(一)

         多加一些測試用例看看.....

public class TestLargest extends TestCase {
	public TestLargest(String name){
		super(name);
	}
	
	public void testSimple(){
		assertEquals(9, Largest.largest(new int[]{9,8,7}));
	}

        public void testSimple1(){
		assertEquals(9, Largest.largest(new int[]{8,9,7}));
	}
	
	public void testSimple2(){
		assertEquals(9, Largest.largest(new int[]{7,8,9}));
	}
}
           

             Oh no no,又有bug了。檢查待測代碼.... 修改源代碼....編譯.....測試.

public class Largest{
	
	public static int largest(int[] list){
		
		int index, max = 0;
		for(index=0; index <= list.length-1; index++){ 
			if(list[index] > max){
				max = list[index];
			}
		}
		
		return max;
	}
}
           

        Green.  Once again! 

Junit 入門(一)
Junit 入門(一)

        應該沒有問題了吧???寫出沒有bug的代碼也許比中500萬都難...還是多加幾個測試看看

import junit.framework.*;
public class TestLargest extends TestCase {
	public TestLargest(String name){
		super(name);
	}
	
	public void testSimple(){
		assertEquals(9, Largest.largest(new int[]{9,8,7}));
	}

	public void testSimple1(){
		assertEquals(9, Largest.largest(new int[]{8,9,7}));
	}
	
	public void testSimple2(){
		assertEquals(9, Largest.largest(new int[]{7,8,9}));
	}
	
	public void testSimple3(){
		assertEquals(9, Largest.largest(new int[]{7,9,8,9}));
	}
	
	public void testSimple4(){
		assertEquals(1, Largest.largest(new int[]{1}));
	}
	
	public void testSimple5(){
		assertEquals(-7, Largest.largest(new int[]{-7,-8,-9}));
	}
}
           

        My god!!!

Junit 入門(一)

 這回是臉都綠了...

Junit 入門(一)

            繼續debug...看來是沒考慮負數...修改源代碼...編譯...測試。

public class Largest{
	
	public static int largest(int[] list){
		
		int index, max = Integer.MIN_VALUE;
		for(index=0; index <= list.length-1; index++){
			if(list[index] > max){
				max = list[index];
			}
		}
		
		return max;
	}
}
           

        OK,心力交瘁,但還是搞定。

Junit 入門(一)

        到此為止,看起來largest可以完美的工作了,但真的完美麼???         如果有人傳入一個空數組的話,largest 的行為又是什麼呢?         是的,應該給它指定一個行為——抛出異常。

public class Largest{
	
	public static int largest(int[] list){
		
		int index, max = Integer.MIN_VALUE;
		
		if(0 == list.length){
			throw new RuntimeException(" Empty list!");
		}
		
		for(index=0; index <= list.length-1; index++){
			if(list[index] > max){
				max = list[index];
			}
		}
		
		return max;
	}
}
           

        加一個測試空數組的用例:

public void testEmpty(){
		try{
			Largest.largest(new int[]{});
			fail("It should be throw exception!");
		}catch (RuntimeException e){
			assertTrue(true);
		}
	}
           

        就這樣,莫名其妙的pass了......

Junit 入門(一)
Junit 入門(一)

        重新審視各個testSimpleX,一種“不進階”的感覺油然而生,原本這些test 隻是測試的值不同而已,那我們能不能将這些值作為參數傳遞給測試用例呢???         答案肯定是可以的.               (世界留給人們的想象空間真的很大???)  

         參數化測試實作:

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;

@RunWith(Parameterized.class)
public class LargestTest {

	private int expected;
	private int[] input;
	
	public LargestTest(int exp, int[] inp){
		this.expected = exp;
		this.input = inp;
	}
	
	@Parameters
	public static Collection preparData(){
		Object[][] object = {{9, new int[]{9,8,7}},{9, new int[]{8,9,7}},{8, new int[]{7,8,9}},{9, new int[]{7,9,8,9}},{1, new int[]{1}},{-7, new int[]{-7,-8,-9}}};
		return Arrays.asList(object);
	}
	@Test
	public void testLargest() {
		assertEquals(expected, Largest.largest(input));  
	}

}