用JUnit4进行参数化测试

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

基本使用方法

  @RunWith

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

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

  @RunWith (Parameterized.class)

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

  

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

文档中的例子

  Class Parameterized的文档中有一个例子:

  For example, to test a Fibonacci function, write:

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

参数化测试实例

  还是以前面的Calculator类作为例子,进行参数化测试:

Calculator

package com.mengdd.junit4;

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

}

参数化测试加法的类:

package com.mengdd.junit4;

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

JUnit4参数的使用的更多相关文章

  1. [转]在Eclipse中使用JUnit4进行单元测试(中级篇)

    我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4 ...

  2. junit4 assert类中的assert方法总结

    junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类. 1.assertTrue/False([String message,]boolean cond ...

  3. 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台

    开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...

  4. 在Eclipse中使用JUnit4进行单元测试(高级篇)

    通过前2篇文章,您一定对JUnit有了一个基本的了解,下面我们来探讨一下JUnit4中一些高级特性. 一.高级Fixture 上一篇文章中我们介绍了两个Fixture标注,分别是@Before和@Af ...

  5. 在Eclipse中使用JUnit4进行单元测试(中级篇)

    我们继续对初级篇中的例子进行分析.初级篇中我们使用Eclipse自动生成了一个测试框架,在这篇文章中,我们来仔细分析一下这个测试框架中的每一个细节,知其然更要知其所以然,才能更加熟练地应用JUnit4 ...

  6. Junit4参数化测试实现程序与用例数据分离

    http://touchfu.iteye.com/blog/732930 现状:你是不是还在为自己的TestCase代码杂乱无章而苦恼,咎其根本还在于针对不同的用例,输入参数和mock信息的组装全部作 ...

  7. Junit3与Junit4的区别

    Junit4最大的亮点就是引入了注解(annotation),通过解析注解就可以为测试提供相应的信息,抛弃junit3使用命名约束以及反射机制的方法. /** * 被测试类 */ package co ...

  8. 使用JUnit4测试Spring

    测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...

  9. Spring 向页面传值以及接受页面传过来的参数的方式

    来源于:http://www.cnblogs.com/liuhongfeng/p/4802013.html 一.从页面接收参数 Spring MVC接收请求提交的参数值的几种方法: 使用HttpSer ...

随机推荐

  1. DataTable Linq Example

    DataTable CreateTable() { DataTable dtable = new DataTable(); DataColumn dc; //MId CId FId PId dc = ...

  2. Swift 08.页面传值

    OC中经常用到的一种初始化控制器的方法.比如A push B.并且将A的一个值:value 传递给B 使用.简单的办法就是 在B重构init方法.比如在.h 中定义: - ()initValue:(N ...

  3. Asp.net页面引用SAP IQ 16 iAnywhere.Data.SQLAnywhere.V4.0.dll报错,语言文件没找到

    参考http://sqlanywhere-forum.sap.com/questions/20420/saconnection-threw-an-exception-cannot-find-the-l ...

  4. HDU 1712 分组背包

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. apache和nginx

    虽然nginx使用较少 还是写写文章,记录下 nginx是异步非阻塞,apache是阻塞的. apache动态页面比nginx好. 由于nginx的高并发性(使用epoll模型),所以出来静态页面性能 ...

  6. NOIP2016之反面教材提供

    NOIP 2016信息竞赛总结 竞赛历程总结: 算下来一共学了11个月的信息竞赛,从最初进来的时候大概会一点最最基础的语法,上课什么也听不懂,然后一直追进度,我想在这个阶段中我的问题主要是自己知道自己 ...

  7. Maven 排除第三方jar包所依赖的其他依赖

    单依赖过滤:可以过滤一个或者多个,如果过滤多个要写多个<exclusion>. <dependency> <groupId>org.apache.hbase< ...

  8. asp.net 5 中应用程序根目录及物理文件根目录的获取方式 此文已过期,不再适应rc1以后的版本

    之前看了asp.net5,小弟就试着用了用,做了个小网站练习一下,有一个小模块需要上传图片到wwwroot下的images文件夹,但是aspnet5 发生了翻天复地变化,之前获取网站根目录的的方法不再 ...

  9. 资源:Python for Windows

    http://win32com.goermezer.de/content/blogsection/7/284/ 一个网站,有各种Windows 下使用 Python 的脚本示例,很不错,有待继续挖掘. ...

  10. matlab 使用札记

    1.inline 命令行内置函数,作用同Function一致 2.repmat B = repmat(A,m,n)B = repmat(A,[m n])B = repmat(A,[m n p...]) ...