用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. CA02检验计划批量导入 模板在文件

    *&---------------------------------------------------------------------* *& PROGRAM NAME(EN) ...

  2. 根据UUID和MD5, 生成可以用作Token的字符串

    import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util. ...

  3. canvas动画

    1.动画主要是requestAnimationFrame方法,现在我们来一步步实现一个在画布内滚动的实例. html代码: <canvas id="canvas" width ...

  4. opencv 基础语法

    1.关于图像:显示图象 //在指定窗口显示图像cvShowImage void cvShowImage( const char* name, const CvArr* image ); name:窗口 ...

  5. Oracle 游标

    游标的简介 游标的概念 游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中的数据进行各种操作 ...

  6. [CentOS] 解决 crontab 无法读取环境变量的问题

    参考资料:http://blog.slogra.com/post-238.html 1. 问题描述 一段数据处理的 shell 程序,在 shell 中手动运行,可以正确执行.但是,把它放在 cron ...

  7. CentOS 6中MATLAB print函数“所见非所得”bug的解决方案

    0 系统配置+软件版本 主机:Dell optiplex 390 MT (i5) 系统+软件:CentOS 6.5 x64, Matlab R2012, R2013 系统+软件:CentOS 6.7 ...

  8. CASS 2008的野外操作码

    表D-1  线面状地物符号代码表 坎类(曲):   K(U) + 数(0-陡坎,1-加固陡坎,2-斜坡,3-加固斜坡,4-垄,5-陡崖,6-干沟) 线类(曲):   X(Q) + 数(0-实线,1-内 ...

  9. twrp编译步骤 (CWM也差不多)

    1.源码的跟目录执行:. build/envsetup.sh,构建编译环境 2.执行 make -j4 otatools,生成后面编译需要的工具 3.把boot或recovery放到home目录,执行 ...

  10. Android studio 提示:Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.参考来源:http://my.oschina.net/fyyy/blog/519353 按照下图,svn相关选项不要选.