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使用参数测试和一组测试的更多相关文章

  1. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  2. JUnit 3.8 通过反射测试私有方法

    测试私有(private)的方法有两种: 1)把目标类的私有方法(修饰符:private)修改为(public),不推荐,因为修改了源程序不佳 2)通过反射 (推荐) 代码演示: 目标程序 Priva ...

  3. Junit使用GroboUtils进行多线程测试

    写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...

  4. 共享参数ContentProvider 类与数据库绑定,如何通过共享参数测试类,测试数据库的增删改查功能

    Intent可以传一个对象 当两个界面之间跳转时,需要传递一个对象过去,是通过使用Bundle类,并且实体类需要serializable实现序列化,传递方法如下: 定义一个静态常量作为key值 pub ...

  5. junit源码解析--捕获测试结果

    OK,前面的博客我们整理了junit运行完了所有的测试用例,那么OK了,现在开始该收集测试结果了. 在这最后一步中,junit主要是玩一个类,TestResult.这里类中封装了几个参数,在初始化这个 ...

  6. Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试

    关于Spring 3.2 1. Spring 3.2 及以上版本自动开启检测URL后缀,设置Response content-type功能, 如果不手动关闭这个功能,当url后缀与accept头不一致 ...

  7. JUnit中按照顺序执行测试方式

    很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前面,然后是修改或者查询,最后才是删除,以 ...

  8. 用junit Test Suite来组合测试

    在测试过程中,有时可能想一次性运行所有的测试类,或是选择性的运行某些测试类.这样的话我们就可以用TestSuite来统一管理我们的测试类. 比如说我现在有三个测试类:junitTest4,TestCa ...

  9. maven插件之maven-surefire-plugin,junit单元测试报告和sonar测试覆盖率的整合说明

    POM中配置的如下: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...

随机推荐

  1. 隐马尔科夫模型(HMM)及事实上现

    马尔科夫模型 马尔科夫模型是单重随机过程,是一个2元组:(S,A). 当中S是状态集合,A是状态转移矩阵. 仅仅用状态转移来描写叙述随机过程. 马尔科夫模型的2个如果 有限历史性如果:t+l时刻系统状 ...

  2. ExcelHelper Excel,Export,Import

    using System; using System.Collections.Generic; using System.Data; using System.Data.Odbc; using Sys ...

  3. Path相关评论的方法(一)

    以前的主要是关于Canvas的translate(平移) .scale(缩放) .rotate(旋转) .skew(错切).接下来几篇主要讲下android里的Path(封装了贝塞尔曲线)&  ...

  4. MySQL Windows ZIP 免费安装和启动设置

    MySQL Windows ZIP免安装版,设置和启动的过程事实上挺麻烦的.以下一步一步介绍使用的过程: 1.下载Windows (x86, 64-bit), ZIP Archive: 2.解压zip ...

  5. Spring官方网站的改版后下载

    Spring官方网站改版很长一段时间后还没有找到直接下载Jar链接包,下面总结了一些方法,可在网上,亲測可用. 1.直接输入地址,改对应版本号就可以:http://repo.springsource. ...

  6. Sqlserver中Over函数

    Over函数不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.  其参数:over(partition by columnname1 order ...

  7. hdu 4308 Saving Princess claire_ BFS

    为了准备算法考试刷的,想明确一点即可,全部的传送门相当于一个点,当遇到一个传送门的时候,把全部的传送门都压入队列进行搜索 贴代码: #include <iostream> #include ...

  8. 猫学习IOS(五岁以下儿童)UI之360其他下载管理器广场UI

    猫分享.必须精品 下载材料:http://blog.csdn.net/u013357243/article/details/44486651 先看效果 主要是完毕了九宫格UI的搭建 代码 - (voi ...

  9. linux下一个oracle11G DG建立(一个):准备环境

    linux下一个oracle11G  DG建立(一个):准备环境 周围环境 名称 主库 备库 主机名 bjsrv shsrv 软件版本号 RedHat Enterprise5.5.Oracle 11g ...

  10. 在JBuilder8在使用ANT

    在JBuilder8中使用ANT                                                            作者:翁驰原    在JBuilder8中.Ap ...