【Java语言生态篇】Junit测试单元

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

01 概述

JUnit是一个由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是白盒测试。JUnit有它自己的JUnit扩展生态圈。多数java的开发环境都已经集成了JUnit作为单元测试的工具。[1]

JUnit是一个开放源代码的Java测试框架,用于编写和运行可重复的测试。他是用于单元测试框架体系xUnit的一个实例(用于java

语言)。它包括以下特性:

1、用于测试期望结果的断言(Assertion)

2、用于共享共同测试数据的测试工具

3、用于方便的组织和运行测试的测试套件

4、图形和文本的测试运行器

测试一般分为:单元测试、集成测试(主要看一块代码加进去后,系统会不会有问题)、验收测试和压力测试。在大点的公司开发

人员每天上班后,第一件事情就是从svn上把自己负责的代码checkout下来,然后运行单元测试,如果单元测试通过,那么说明自己的

代码没有问题,然后就在代码块上修改与添加,完成后再用junit进行测试,测试完成后如果没有问题,那么就把相应的代码块提交给

svn上。[2]

02 Junit配置

环境:Eclipse 中配置junit,

选择项目Junit4(在Package Explorer中)右击鼠标选择PRoperties选择Java Build Path (在左边的菜单中)在右边选择标签 Libraries单击按钮“Add Library” 选择JUnit , 单击按钮 “Next>” 选择JUnit library version 为: JUnit4单击按钮“Finish”单击按钮 “OK”或手动 添加Junit.jar和hamcrest-core-xx.jar,hamcrest-library-xx.jar (或hamcrest-all-xx.jar)。(建议手动添加,在eclipse中通过前一种方法只引入Junit-xx.jar 和 hamcrest-core-xx.jar)

03 Junit3 和 Junit4的区别

Junit3必须引入和继承TestCaseJunit3 测试方法必须以test开头Junit3 测试方法前后的执行方法必须名为setUp() 和 tearDown()Junit4 不需要引入和继承TestCase。采用注解,在方法前加入@TestJunit4 测试方法不需要强制为test开头Junit4 测试方法前后执行方法不需要强制命名为setUp() 和 tearDown() ,只需要添加注解@Before和@AfterJunit4 中添加了异常测试04 Junit4的几个常用注解[3]

Junit 4 的单元测试用例执行顺序为:

@BeforeClass –> @Before –> @Test –> @After –> @AfterClass;

每一个测试方法的调用顺序为:@Before –> @Test –> @After。

--------------------------------------------------------------------

@Before:初始化方法,在任何一个测试执行之前必须执行的代码;@After:释放资源,在任何测试执行之后需要进行的收尾工作。在每个测试方法执行之后执行一次,该annotation只能修饰public void 方法;@Test:测试方法,表明这是一个测试方法。在Junit中将会自动被执行。该annotation只能够修饰public void 方法。对于方法的声明也有如下要求:名字可以随便取,没有任何限制,但是返回值必须为void,而且不能有任何参数。如果违反这些规定,会在运行时抛出一个异常。至于方法内该写些什么,那就要看你需要测试些什么了;在这里可以测试期望异常和超时时间,如 @Test(timeout = 100):我们给测试函数设定一个执行时间,超过了这个时间(100毫秒),它们就会被系统强行终止,并且系统还会向你汇报该函数结束的原因是因为超时,这样你就可以发现这些Bug了。@Ignore:忽略的测试方法,标注的含义就是“某些方法尚未完成,暂不参与此次测试”;这样的话测试结果就会提示你有几个测试被忽略,而不是失败。一旦你完成了相应函数,只需要把@Ignore标注删去,就可以进行正常的测试。@BeforeClass:针对所有测试,只执行一次,且必须为public static void;@AfterClass:针对所有测试,将会在所有测试方法执行结束后执行一次,且必须为public static void;05 hamcrest 和 testSuite 介绍[4]

hamcrest就是专门为增强junit来提供的框架。Junit中,主要使用assert做一些基本的判断。但很多时候使用assert做判断,并不方便,如果要判断某几个值是否为true或false,这时使用hamcrest来判断就会方便许多。

在一个项目中,可能有很多的测试类,如果每个测试类都要点击运行,那么成百上千个类都需要测试,这会是个比较繁重的工作,这时可以使用可以使用junit的jar包中提供的Suite来解决这个问题。

06 综合实例

TestCalcuate类

import static org.junit.Assert.*;

import static org.hamcrest.Matchers.*;

import org.junit.Before;

import org.junit.Test;

public class TestCalcuate {

Calcuate cal;

@Before

public void setUp(){

cal=new Calcuate();

}

@Test

public void testAdd(){

int rel=cal.add(22, 33);

assertEquals("加法存在问题",rel, 55);

}

@Test

public void testMinus(){

int rel=cal.minus(33, 11);

assertEquals("减法存在问题",rel,22);

}

@Test(expected=ArithmeticException.class)

public void testDivid(){

int rel=cal.divide(22, 0);

assertEquals("除法存在问题",rel,2);

}

@Test

public void testMul(){

int rel=cal.mul(10, 20);

assertEquals("乘法存在问题", rel,200);

}

@Test(timeout=1000)

public void testTime(){

try{

Thread.sleep(200);

}catch(Exception e){

e.printStackTrace();

}

int rel=cal.mul(10, 20);

assertEquals("乘法存在问题", rel,200);

}

@Test

public void testHam(){

assertThat(50, allOf(greaterThan(49),lessThan(60)));

assertThat(60, allOf(greaterThan(40),lessThan(90)));

assertThat("like.txt", endsWith(".txt"));

}

}

TestA类

import org.junit.*;

public class TestA {

@Test

public void testA(){

System.out.println("testA");

}

}

TestB类

import org.junit.*;

public class TestB {

@Test

public void testB(){

System.out.println("testB");

}

}

TestSuite类

import org.junit.Test;

import org.junit.runner.RunWith;

import org.junit.runners.Suite;

import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)

@SuiteClasses({TestA.class,TestB.class,TestCalcuate.class})

public class TestSuite {

@Test

public void testSuite(){

System.out.println("testSuite");

}

}

07 其它问题

1、测试原则:[4]

建议创建一个专门的source folder--->test来编写测试类代码。上面例子就新建一个test的资源包。测试类的包应该保持和需要测试的类一致。上面的例子中Calcuate类在src/cn/whp/util包中,相应的测试类就在test/cn/whp/util中。测试单元中的每个测试方法都必须可以独立执行,不相互依赖,没有顺序。

2、用junit在进行单元测试的时候,使用assertThat方法,发现hamcrest的greaterThan等方法无法识别【说是没有定义】,而且无法静态引用到Matchers类【import static org.hamcrest.Matchers.*;】[5]

原因:因为junit默认只会依赖hamcrest-core-1.3包,而实际上hamcrest还有一个hamcrest-library.jar用来放置很多工具

参考文献

[1] 百度百科 Junit

[2] 悔海http://huihai.iteye.com/blog/1986568

[3] 小倩的博客http://blog.sina.com.cn/s/blog_8354dda80101ee8v.html

[4] 悔海http://huihai.iteye.com/blog/1994270

[5] http://www.fwqtg.net/junit-使用hamcrest的matcher找不到的问题(比如greaterthan).html

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航