天天看点

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));  
	}

}