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> ...
随机推荐
- python学习之list
list: 创建:list = [5,7,9] 取值和改值:list[1] = list[1] * 5 列表尾插入:list.append(4) 去掉第0个值并返回第0个值的数值:list.pop(0 ...
- HTTP代理与SPDY协议(转)
原文出处: fqrouter HTTP代理是最经典最常见的代理协议.其用途非常广泛,普遍见于公司内网环境,一般员工都需要给浏览器配置一个HTTP代理才能访问互联网.起初,HTTP代理也用来翻越“功夫网 ...
- hadoop调度程序时出现“Error opening job jar”错误
提示出现的问题: Exception in thread "main" java.io.IOException: Error opening job jar: /home/depl ...
- js控制图片缩放、水平和垂直方向居中对齐
已測试兼容 IE6,IE7,IE8,火狐FF,谷歌chrome. 这里使用了jquery插件,假设你不使用jquery,略微改造一下也非常快. 网上查了些资料,用css控制兼容性不好,看去非常揪心.于 ...
- 慎重使用MySQL auto_increment
在使用MySQL中,常常会在表中建立一个自增的ID字段,利用自增ID可以高速建立索引,也是MySQL官方比較推荐的一种方式,可是,这样的方式在大量数据且配置主从时,可能会出现因为自增ID导致同步失败的 ...
- Android锁定屏幕或关闭状态-screen,高速按两次音量向下键来实现拍摄功能(1.1Framework在实现的形式层广播)
思想的实现: WindowManagerService循环读取下面的关键信息和分发形式.在PhoneWindowManager.interceptKeyBeforeQueueing方法中进行消 ...
- Byte[]和BASE64之间的转换
一. BASE64编码 把byte[]中的元素当做无符号八位整数转换成只含有64个基本字符的字符串,这些基本字符是: l 大写的A-Z l 小写的a-z l 数字0-9 l '+' 和 '/' l 空 ...
- 玩转Web之easyui(三)-----easy ui dataGird 重新指定url以获取不同数据源信息
如果已经写了一个dataGird并且已经通过url绑定数据源,能不能在其他地方改变url使其从不同数据源获取信息,从而实现查询等操作?答案当然是肯定的,而且仅需要几行代码 $('#btnq').bin ...
- AsyncActivity异步加载网页
import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; imp ...
- (五岁以下儿童)NS3样本演示:桥模块演示样品csma-bridge.cc凝视程序
(五岁以下儿童)NS3:桥模块演示样品csma-bridge.cc凝视程序 1.Ns3 bridge模csma-bridge.cc演示示例程序的目光 // Network topology // // ...