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> ...
随机推荐
- unity3d教程动态创建简单平面地形
unity3d创建地形是不须要usingUnityEditor的.这里使用了AssetDatabase.所以需using UnityEditor; 创建三步: 1.TerrainData terrai ...
- java日历程序版本
//MainFrame.java package com.huowolf.myCalender; import java.awt.BorderLayout; import java.awt.Color ...
- html网页特殊符号代码
HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ...
- MD5加密,解密
using System; using System.Collections.Generic; using System.Text; using System.Globalization; using ...
- Linux下一个C(编程入门.h档,.c档,而路多文件的调用)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0 ...
- NYOJ 709(ZZULIOJ1481) 异 形 卵
题目描写叙述 我们探索宇宙,是想了解浩瀚星空的奥妙,但我们却非常少意识到宇宙深处藏匿的危急,它们无时无刻不紧盯着我们的地球.假设外星人拜訪我们,结果可能与哥伦布当年踏足美洲大陆不会有什么两样,这是历史 ...
- 1104. Don’t Ask Woman about Her Age(数论)
a*b^n(mod(b-1) =a(mod(b-1) http://acm.timus.ru/problem.aspx?space=1&num=1104 #include <stdio. ...
- HOJ2275 Number sequence
Number sequence My Tags tag=&type=or" style="margin:0px; padding:0px; color:rgb(27,87, ...
- 优化移动设备上SharePoint 2013网站
优化移动设备上SharePoint 2013网站 本文由SPFarmer翻译自Waldek Mastykarz的文章 移动市场在持续的增长.在不远的将来,使用移动设备浏览站点将会超过电脑.为了保证用户 ...
- 好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效button
我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name=& ...