用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));
} }
参考资料
圣思园张龙老师视频教程。
JUnit4 chm格式文档网盘下载链接:
JUnit 4.0:http://pan.baidu.com/share/link?shareid=539345&uk=2701745266
用JUnit4进行参数化测试的更多相关文章
- Junit4进行参数化测试
@RunWith, 当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner)来运行测试,而不是JUnit默认的运行器. 要进行参数化测 ...
- 同时使用Junit4的@Parameterized参数化测试和Spring容器
转载:http://www.jianshu.com/p/d191fe54915f 整合Spring容器 @SpringApplicationConfiguration(classes = Applic ...
- Junit4参数化测试实现程序与用例数据分离
http://touchfu.iteye.com/blog/732930 现状:你是不是还在为自己的TestCase代码杂乱无章而苦恼,咎其根本还在于针对不同的用例,输入参数和mock信息的组装全部作 ...
- JUnit4参数化测试实例
在JUnit中,可以同时使用@RunWith 和 @parameter 注解来为单元测试传递参数. 注意: 在Eclipse中因为版本问题,可能无法使用@parameters(name = " ...
- junit 单元测试 - 参数化测试
junit4.x版本需要引入如下jar包: hamcrest-core-1.3.jar junit-4.12-beta-3.jar 新建一个计算器类,如下: package com.pt; publi ...
- junit参数化测试
在前面的junit4初体验中我就说过,junit参数化测试是一只小怪兽,只逼编码痛点,现在我们这里来整理一下. 看过我前面的那篇初体验的就会发现一个问题,我们的测试代码大量的重复了.在这里先贴出原来的 ...
- 参数化测试与Mock
参数化测试与Mock 转载自https://blog.csdn.net/sunliduan/article/details/42026509 单元测试概念 说到测试,大家都不会陌生,从我们开始学习编程 ...
- Junit5中实现参数化测试
从Junit5开始,对参数化测试支持进行了大幅度的改进和提升.下面我们就一起来详细看看Junit5参数化测试的方法. 部署和依赖 和Junit4相比,Junit5框架更多在向测试平台演进.其核心组成也 ...
- 计算某天的下一天:黑盒测试之等价类划分+JUnit参数化测试
题目要求 测试以下程序:该程序有三个输入变量month.day.year(month.day和year均为整数值,并且满足:1≤month≤12.1≤day≤31和1900≤year≤2050),分别 ...
随机推荐
- HDU 3697 Selecting courses(贪心)
题目链接:pid=3697" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=3697 Prob ...
- [RxJS] Updating Data with Scan
You often need to update the data flowing through the stream with custom logic based on what you nee ...
- nginx使用keepalived实现高可用
环境: 主:linux-node1 110.0.0.137 备:linux-node2 110.0.0.138 VIP: 110.0.0.120 NGINX安装: # rpm -ivh h ...
- WPF bitmap转bitmapimage 使用 CreateBitmapSourceFromHBitmap内存泄漏
IntPtr f = bmp.GetHbitmap(); img.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitm ...
- ashx ajax 与 自定义javascript函数
1.getUserPower为自定义javascript函数 获取权限 (1).ashx 处理程序的相对地址(必须是相对地址) (2).au 权限名称 (3).classname 类名 (4) ...
- 第一次写博客,关于前端开发deMVC在js中的应用
对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...
- js处理数学经典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- ajax请求或者页面需要缓存,代码如下
缓存页面代码 1 private void setClientCache(HttpServletResponse response, int maxAgeSec) 2 { 3 long maxAgeM ...
- STL vector 用法介绍
介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...
- (原)python中import caffe提示no module named google.protobuf.internal
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5993405.html 之前在一台台式机上在python中使用import caffe时,没有出错.但是 ...