JUnit使用参数测试和一组测试
JUnit该参数测试和一组测试使用简单
参数测试
作为替代阵列int a0,a1,a2喜欢,当测试加法assertEquals(3.0, h.add(1, 2), 0.1);相当于声明一个变量,要測试100个怎么办。
所以,有了參数化測试,使用一个Collection收集全部的数据——加法时每一次測试须要的几个数据构成一组,n个组构成Collection。
然后依照JUnit的使用方法要求,写出单元測试类。(偷懒一下,不想写被測试的业务类X了。
以下的样例中如果要測试的方法是,推断一个数是否奇数。)
package myTest.param;
import java.util.Arrays;
import java.util.Collection; import org.junit.Test;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class)//1.必须
public class ParametTestUnit {
private int input;
private boolean expected;//expected result
/**
* 2.public 构造器赋值一组測试数据
*/
public ParametTestUnit(int input,boolean expected ) {
this.input = input;
this.expected = expected;
} /**
* 3.由@Parameterized.Parameters修饰一个
* public static Collection xxx()
*/
@Parameterized.Parameters
public static Collection data() {
return Arrays.asList(new Object[][] {
{ 1, true },
{ 3, true },//
{ 6, false },
{ 11, true },
{ 22, false },
{ 23, true }
});
} /**
* 4.JUnit循环地使用各组数据
*/
@Test
public void testOdd() {
System.out.println("Parameterized Number is : " + input);
assertEquals(expected, input%2!=0);
}
}
如今这个单元測试类编写完毕,可是在BlueJ中不可以直接执行它(不支持?)。自己写一个Main好了。
package myTest.param;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure; public class Main {
public static void go() {
Result result = JUnitCore.runClasses(ParametTestUnit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
执行结果:
Parameterized Number is : 1
Parameterized Number is : 3
Parameterized Number is : 6
Parameterized Number is : 11
Parameterized Number is : 22
Parameterized Number is : 23
true
如今,将某个数据改动一下,如{ 11, true }改成{ 10, true },执行结果:
Parameterized Number is : 1
Parameterized Number is : 3
Parameterized Number is : 6
Parameterized Number is : 10
Parameterized Number is : 22
Parameterized Number is : 23
testOdd[3](myTest.param.ParametTestUnit): expected:<true> but was:<false>
false
表示第3个数据(基于0)有问题。
成组測试
有非常多单元測试类须要測试,将它们组成一个Suite。大家一起測试。
比如新写了两个单元測试类:
package myTest.param;
import static org.junit.Assert.assertEquals;
public class AddUnit1 {
@org.junit.Test
public void testAdd() {
System.out.println("Inside AddUnit1");
assertEquals(3, 1+2);
}
}
package myTest.param;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
public class AddUnit2 {
@org.junit.Test
public void testSth() {
System.out.println("Inside AddUnit2");
assertThat("Zero is one", 0, is(not(1))); // passes
assertThat("Zero is one", 0, is(1)); //fail
}
}
将它们组成一个Suite,须要写捆绑代码,清晰起见。单独用一个类。
package myTest.param;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
AddUnit1.class,
AddUnit2.class,
//ParametTestUnit.class
})
public class SuiteUnit {}
还是用上面的Main,改动为runClasses(SuiteUnit.class); 执行结果:
Inside AddUnit1.testAdd()
Inside AddUnit2.testSth()
testSth(myTest.param.AddUnit2): Zero is one
Expected: is <1>
got: <0>
false
能够把对ParametTestUnit.class的測试也加进来。执行结果:
Inside AddUnit1.testAdd()
Inside AddUnit2.testSth()
Parameterized Number is : 1
Parameterized Number is : 3
Parameterized Number is : 6
Parameterized Number is : 10
Parameterized Number is : 22
Parameterized Number is : 23
testSth(myTest.param.AddUnit2): Zero is one
Expected: is <1>
got: <0>
testOdd[3](myTest.param.ParametTestUnit): expected:<true> but was:<false>
false
版权声明:本文博客原创文章。博客,未经同意,不得转载。
JUnit使用参数测试和一组测试的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- JUnit 3.8 通过反射测试私有方法
测试私有(private)的方法有两种: 1)把目标类的私有方法(修饰符:private)修改为(public),不推荐,因为修改了源程序不佳 2)通过反射 (推荐) 代码演示: 目标程序 Priva ...
- Junit使用GroboUtils进行多线程测试
写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...
- 共享参数ContentProvider 类与数据库绑定,如何通过共享参数测试类,测试数据库的增删改查功能
Intent可以传一个对象 当两个界面之间跳转时,需要传递一个对象过去,是通过使用Bundle类,并且实体类需要serializable实现序列化,传递方法如下: 定义一个静态常量作为key值 pub ...
- junit源码解析--捕获测试结果
OK,前面的博客我们整理了junit运行完了所有的测试用例,那么OK了,现在开始该收集测试结果了. 在这最后一步中,junit主要是玩一个类,TestResult.这里类中封装了几个参数,在初始化这个 ...
- Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试
关于Spring 3.2 1. Spring 3.2 及以上版本自动开启检测URL后缀,设置Response content-type功能, 如果不手动关闭这个功能,当url后缀与accept头不一致 ...
- JUnit中按照顺序执行测试方式
很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前面,然后是修改或者查询,最后才是删除,以 ...
- 用junit Test Suite来组合测试
在测试过程中,有时可能想一次性运行所有的测试类,或是选择性的运行某些测试类.这样的话我们就可以用TestSuite来统一管理我们的测试类. 比如说我现在有三个测试类:junitTest4,TestCa ...
- maven插件之maven-surefire-plugin,junit单元测试报告和sonar测试覆盖率的整合说明
POM中配置的如下: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...
随机推荐
- 深入浅出KnockoutJS
深入浅出KnockoutJS 写在前面,本文资料大多来源网上,属于自己的学习笔记整理. 其中主要内容来自learn.knockoutjs.com,源码解析部分资料来自司徒正美博文<knockou ...
- SVN 右下角各种符号
黄色感叹号(有冲突): --这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不 允许你提交,防止你的提交覆盖了 ...
- windows phone 三种数据共享的方式(8)
原文:windows phone 三种数据共享的方式(8) 本节实现的内容是数据共享,实现的效果描述:首先是建立两个页面,当页面MainPage通过事件导航到页面SecondPage是,我们需要将Ma ...
- iOS游戏开发游戏功能之外的东西
对于一个游戏的开发,我们除了完毕游戏的功能之外,还有多少东西我们须要考虑呢? 非常多.也非常烦! 但做过一遍之后下一次就会非常easy. 都有什么东西我们想加入到游戏其中呢? (1)分享功能 (2)评 ...
- leetcode第一刷_Pow(x, n)
高速乘方的算法,写了好多变,出了各种错,真是服了我自己了. 思想是每次对n减半,将当前的temp平方.须要注意的是假设当前的n是个奇数,减半之后会丢失掉一次乘积,因此假设当前的n为奇数,应该先在结果里 ...
- ZOJ1093 动态规划
给你n砖,有三个长宽高.每一个无限制的访问.叠加在一个条件的长度和宽度必须严格格长度和宽度大于下面的一个,叠加求最大高度. 思维: 每块砖终于放置在根据本方法可以把六种,然后,对于长度和宽度排序.这是 ...
- lua 远程调试 【zeroBrane 使用mobdebug】(good转)
最近基于业务需求,学习了如何使用zeroBrane这个IDE实现C/S 模式下的 lua远程调试,废话不多,上效果图: ---------------------------------------- ...
- 每天进步一点点——再次了解Linux进程ID
转载请注明出处:http://blog.csdn.net/cywosp/article/details/38968011 1. 概述 众所周知,进程(process)是一个可运行程序的实例,可是在Li ...
- iterator pattern
6 迭代器模式总结 迭代器模式是一种使用频率非常高的设计模式,通过引入迭代器可以将数据的遍历功能从聚合对象中分离出来,聚合对象只负责存储数据,而遍历数据由迭代器来完成.由于很多编程语言的类库都已经实现 ...
- Linux - SVN下载项目
SVN下载项目 本文地址:http://blog.csdn.net/caroline_wendy 使用SVN.在Git上下载项目. $ mkdir chunyu_trunk //创建目录 $ ls / ...