`

用JUnit4进行参数化测试

阅读更多

        参数化测试是一个JUnit 3不具备的功能。

基本使用方法:

一.@RunWith

  当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不是JUnit默认的运行器。

文档中的例子

        Class Parameterized的文档中有一个Fibonacci(斐波那契)的例子:

        斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)。

  For example, to test a Fibonacci function, write:

FibonacciTest.java

package com.bijian.study;

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 FibonacciTest {
	
	@Parameters
	public static Collection data() {
		return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
				{ 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
	}

	private int fInput;
	private int fExpected;

	public FibonacciTest(int input, int expected) {
		fInput = input;
		fExpected = expected;
	}

	@Test
	public void test() {
		assertEquals(fExpected, Fibonacci.compute(fInput));
	}
}

Fibonacci.java

package com.bijian.study;

public class Fibonacci {

	public static int compute(int n) {
		if (n < 0) {
			return -1;
		}
		if (n == 0 || n == 1) {
			return n;
		}
		return compute(n - 2) + compute(n - 1);
	}
}

单元测试结果:



二.@RunWith (Parameterized.class)

       要进行参数化测试,需要在类上面指定如下的运行器:

@RunWith (Parameterized.class)

  然后,在提供数据的方法上加上一个@Parameters注解,这个方法必须是静态static的,并且返回一个集合Collection。

  在测试类的构造方法中为各个参数赋值,(构造方法是由JUnit调用的),最后编写测试类,它会根据参数的组数来运行测试多次。

参数化测试实例:还以Calculator类作为例子

Calculator.java

package com.bijian.study;

public class Calculator {
	public int add(int a, int b) {
		return a + b;
	}

	public int subtract(int a, int b) {
		return a - b;
	}

	public int multiply(int a, int b) {
		return a * b;
	}

	public int divide(int a, int b) throws Exception {
		if (0 == b) {
			throw new Exception("除数不能为0");
		}
		return a / b;
	}
}

参数化测试加法的类:ParametersTest.java

package com.bijian.study;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
// 指定运行器runner:使用参数化运行器来运行
public class ParametersTest {
	private int expected;// 期待的结果值

	private int input1;// 参数1

	private int input2;// 参数2

	private Calculator calculator = null;

	@Parameters
	public static Collection prepareData() {
		// 测试数据
		Object[][] objects = { { 3, 1, 2 }, { -4, -1, -3 }, { 5, 2, 3 },
				{ 4, -4, 8 } };
		return Arrays.asList(objects);// 将数组转换成集合返回

	}

	@Before
	public void setUp() {
		calculator = new Calculator();
	}

	public ParametersTest(int expected, int input1, int input2) {
		// 构造方法
		// JUnit会使用准备的测试数据传给构造函数
		this.expected = expected;
		this.input1 = input1;
		this.input2 = input2;
	}

	@Test
	public void testAdd() {
		Assert.assertEquals(this.expected, calculator.add(this.input1,
				this.input2));
	}
}

单元测试结果:

 

文章来源:http://www.cnblogs.com/mengdd/archive/2013/04/13/3019336.html

  • 大小: 23.6 KB
  • 大小: 20.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics