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. selenium 远程调用浏览器

    共分三步: 1.selenium官网下载selenium-server-standalone.jar的最新版本号 2.启动selenium-server::::: java -jar "se ...

  2. windows下php开发环境的搭建

    环境搭建软件组合为:Apache2.2.9+mysql5.2.32+php5.2.6  下载地址如下 http://download.csdn.net/detail/xttxqjfg/5670455 ...

  3. hdu1532 (最大流入门,EK算法)

    看着这个博客 然后敲了hdu1532这个入门题,算是对最大流有点理解了 #include <stdio.h> #include <string.h> #include < ...

  4. Java 理论与实践: 处理 InterruptedException(转)

    很多 Java™ 语言方法,例如 Thread.sleep() 和 Object.wait(),都可以抛出InterruptedException.您不能忽略这个异常,因为它是一个检查异常(check ...

  5. Java使用LdAP获取AD域用户

    随着我们的习大大上台后,国家在网络信息安全方面就有了非常明显的改变!所以如今好多做网络信息安全产品的公司和须要网络信息安全的公司都会提到用AD域server来验证,这里就简单的研究了一下! 先简单的讲 ...

  6. shell程序之逐行读取一文件里的參数且使用此參数每次运行5分钟

    /*********************************************************************  * Author  : Samson  * Date   ...

  7. 【JUnit4.10来源分析】0导航

    JUnit靠GOF 中的一个Erich Gamma和 Kent Beck 单元测试框架编写一个开源,yqj2065分析JUnit的主要目的是源 中学习对设计模式的运用. JUnit也是一个学习Java ...

  8. Perl中的单行凝视和多行凝视

    同其它大多数编程语言一样.Perl中的单行凝视也是#开头.比如: #print "Hello,World!"; 但多行凝视.不同的语言有不同的凝视方式,比方说: Java,C/C+ ...

  9. CSDN挑战编程——《金色十月线上编程比赛第二题:解密》

    金色十月线上编程比赛第二题:解密 题目详情: 小强是一名学生, 同一时候他也是一个黑客. 考试结束后不久.他吃惊的发现自己的高等数学科目竟然挂了,于是他果断入侵了学校教务部站点. 在入侵的过程中.他发 ...

  10. unrecognized selector sent to instance 0x10b34e810

    一个错误: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLEr ...