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

基本使用方法

  @RunWith

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

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

  @RunWith (Parameterized.class)

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

  

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

文档中的例子

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

  For example, to test a Fibonacci function, write:

  1. @RunWith(Parameterized.class)
  2. public class FibonacciTest
  3. {
  4. @Parameters
  5. public static Collection data()
  6. {
  7. return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
  8. { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
  9. }
  10.  
  11. private int fInput;
  12. private int fExpected;
  13.  
  14. public FibonacciTest(int input, int expected)
  15. {
  16. fInput = input;
  17. fExpected = expected;
  18. }
  19.  
  20. @Test
  21. public void test()
  22. {
  23. assertEquals(fExpected, Fibonacci.compute(fInput));
  24. }
  25. }

参数化测试实例

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

  1. Calculator
  2.  
  3. package com.mengdd.junit4;
  4.  
  5. public class Calculator
  6. {
  7. public int add(int a, int b)
  8. {
  9. return a + b;
  10. }
  11.  
  12. public int subtract(int a, int b)
  13. {
  14. return a - b;
  15. }
  16.  
  17. public int multiply(int a, int b)
  18. {
  19. return a * b;
  20. }
  21.  
  22. public int divide(int a, int b) throws Exception
  23. {
  24. if (0 == b)
  25. {
  26. throw new Exception("除数不能为0");
  27. }
  28. return a / b;
  29. }
  30.  
  31. }

参数化测试加法的类:

  1. package com.mengdd.junit4;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5.  
  6. import org.junit.Assert;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.junit.runners.Parameterized;
  11. import org.junit.runners.Parameterized.Parameters;
  12.  
  13. @RunWith(Parameterized.class)
  14. // 指定运行器runner:使用参数化运行器来运行
  15. public class ParametersTest
  16. {
  17. private int expected;// 期待的结果值
  18.  
  19. private int input1;// 参数1
  20.  
  21. private int input2;// 参数2
  22.  
  23. private Calculator calculator = null;
  24.  
  25. @Parameters
  26. public static Collection prepareData()
  27. {
  28. // 测试数据
  29. Object[][] objects = { { 3, 1, 2 }, { -4, -1, -3 }, { 5, 2, 3 },
  30. { 4, -4, 8 } };
  31. return Arrays.asList(objects);// 将数组转换成集合返回
  32.  
  33. }
  34.  
  35. @Before
  36. public void setUp()
  37. {
  38. calculator = new Calculator();
  39. }
  40.  
  41. public ParametersTest(int expected, int input1, int input2)
  42. {
  43. // 构造方法
  44. // JUnit会使用准备的测试数据传给构造函数
  45. this.expected = expected;
  46. this.input1 = input1;
  47. this.input2 = input2;
  48. }
  49.  
  50. @Test
  51. public void testAdd()
  52. {
  53. Assert.assertEquals(this.expected,
  54. calculator.add(this.input1, this.input2));
  55. }
  56.  
  57. }

参考资料

  圣思园张龙老师视频教程。

  JUnit4 chm格式文档网盘下载链接:

  JUnit 4.0:http://pan.baidu.com/share/link?shareid=539345&uk=2701745266

用JUnit4进行参数化测试的更多相关文章

  1. Junit4进行参数化测试

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

  2. 同时使用Junit4的@Parameterized参数化测试和Spring容器

    转载:http://www.jianshu.com/p/d191fe54915f 整合Spring容器 @SpringApplicationConfiguration(classes = Applic ...

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

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

  4. JUnit4参数化测试实例

    在JUnit中,可以同时使用@RunWith 和 @parameter 注解来为单元测试传递参数. 注意: 在Eclipse中因为版本问题,可能无法使用@parameters(name = " ...

  5. junit 单元测试 - 参数化测试

    junit4.x版本需要引入如下jar包: hamcrest-core-1.3.jar junit-4.12-beta-3.jar 新建一个计算器类,如下: package com.pt; publi ...

  6. junit参数化测试

    在前面的junit4初体验中我就说过,junit参数化测试是一只小怪兽,只逼编码痛点,现在我们这里来整理一下. 看过我前面的那篇初体验的就会发现一个问题,我们的测试代码大量的重复了.在这里先贴出原来的 ...

  7. 参数化测试与Mock

    参数化测试与Mock 转载自https://blog.csdn.net/sunliduan/article/details/42026509 单元测试概念 说到测试,大家都不会陌生,从我们开始学习编程 ...

  8. Junit5中实现参数化测试

    从Junit5开始,对参数化测试支持进行了大幅度的改进和提升.下面我们就一起来详细看看Junit5参数化测试的方法. 部署和依赖 和Junit4相比,Junit5框架更多在向测试平台演进.其核心组成也 ...

  9. 计算某天的下一天:黑盒测试之等价类划分+JUnit参数化测试

    题目要求 测试以下程序:该程序有三个输入变量month.day.year(month.day和year均为整数值,并且满足:1≤month≤12.1≤day≤31和1900≤year≤2050),分别 ...

随机推荐

  1. Web Service-- 使用 JDK 发布 WS

    Web Service,即“Web 服务”,简写为 WS,从字面上理解,它其实就是“基于 Web 的服务”.而服务却是双方的,有服务需求方,就有服务提供方.服务提供方对外发布服务,服务需求方调用服务提 ...

  2. 漫话Unity3D(一)

    前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...

  3. hdfs经常使用命令

    hadoop hdfs经常使用命令 hadoop fs -ls /user/deploy/recsys/workspace/ouyangyewei 查看ouyangyewei文件夹文件 hadoop ...

  4. swift调用相机和相册

    简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...

  5. python 之 Paramiko学习

    paramiko模块,基于SSH用于连接远程服务器并执行相关操作. 一.安装 pip3 install paramiko 二.使用 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码 ...

  6. IE6,IE7下滚动条没有生效解决方法

    需要加个相对定位 position:relative;

  7. UIView设置少于四个的圆角

    最近的需求中有个label需要设置右下角为圆角,其余三个为直角,一开始用的是重写drawRect,然后用绘图重绘每个角的样子,计算起来还是麻烦 后来发现了下面的方法: UILabel *courseS ...

  8. HDU 1269 裸奔的强联通分量

    看了别人博客  http://blog.csdn.net/jokes000/article/details/7538994 #include <cstdio> #include <c ...

  9. 为什么memset不能将数组元素初始化为1?

    原型:extern void *memset(void *buffer, int c, int count); 功能:把buffer所指内存区域的前count个字节设置成字符c. 包含头文件:< ...

  10. Mahout快速入门教程

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...