用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. VA01复制单据,更新定价日期和价格

    用户经常复制单据,而复制单据的时候会带过来很多日期的历史数据.定价日期就是其中之一,而价格经常变动,或者删除的话,会出现价格错误等等情况. 1.更新定价日期,保证不会使用历史价格. 2.更新价格,保证 ...

  2. Json.net 常用使用小结

    using System; using System.Linq; using System.Collections.Generic; namespace microstore { public int ...

  3. 关于ADO.NET 超时的问题

    前几天超时问题困扰我很头疼. 为什么我设置了链接字符串的超时时间很长,可是等了一小会就报错Timeout了? connectionString="Data Source=.;Initial ...

  4. guava学习--FutureCallback

    转载:https://my.oschina.net/realfighter/blog/349929 Guava提供了 FutureCallback接口,FutureCallback接口提供了onSuc ...

  5. ASP.NET MVC为字段设置多语言显示 [转]

    这段时间一直在忙.NET的项目,今天就写一下关于.NET的文章吧,也很长时间没写过.NET的啦  在使用ASP.NET MVC3 的时候,使用元数据模式可以非常方便地设置每个 字段(或者说属性)以减少 ...

  6. erlang 查看内存消耗的方法?

    找出消耗内存最多的进程 : lists:reverse(lists:keysort(2,[{P, erlang:process_info(P, heap_size)} || P <- erlan ...

  7. 如何查询postgresql+openstreetmap

    先行输入:psql gis \d 显示当前数据表 List of relations Schema | Name | Type | Owner --------+------------------- ...

  8. activeMq 消费者整合spring

    package com.mq.consumer; import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Mes ...

  9. 笔记:linux下mysql设置utf-8编码方法

    一:查看mysql版本 1.1 mysql –V 在终端界面输入上面命令.显示如下: mysql Ver 14.14 Distrib 5.5.35, fordebian-linux-gnu (x86_ ...

  10. js正则实现二代身份证号码验证详解

    js正则实现二代身份证号码验证详解 根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至 ...