junit使用简明手册
上一篇 / 下一篇 2011-01-21 15:12:45 / 个人分类:单元测试
使用目的
junit是java中书写unit test的framework,目前一些流行的unit test工具大都都是在junit上扩展而来的。本文针对的的版本是junit3.8.1,可以从www.junit.org上下载。最新版本是junit4.1,一般最新的myeclipse开发环境中都集成了的。
- First, download the latest version of JUnit, referred to below as
junit.zip
. - Then install JUnit on your platform. of choice:
To install JUnit on Windows, follow these steps:
- Unzip the
junit.zip
distribution file to a directory referred to as%JUNIT_HOME%
. - Add JUnit to the classpath:
set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\junit.jar
Unix (bash)
To install JUnit on Unix, follow these steps:
- Unzip the
junit.zip
distribution file to a directory referred to as$JUNIT_HOME
. - Add JUnit to the classpath:
export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit.jar
- Unzip the
- (Optional) Unzip the
$JUNIT_HOME/src.jar
file. - Test the installation by using either the textual or graphical test runner to run the sample tests distributed with JUnit.
Note: The sample tests are not contained in the
junit.jar
, but in the installation directory directly. Therefore, make sure that the JUnit installation directory is in the CLASSPATH.For the textual TestRunner, type:
java junit.textui.TestRunner junit.samples.AllTests
For the graphical TestRunner, type:
java junit.swingui.TestRunner junit.samples.AllTests
All the tests should pass with an "OK" (textual) or a green bar (graphical).
If the tests don't pass, verify that
junit.jar
is in the CLASSPATH. - Finally, read the documentation.
用法
1. 基本使用步骤,Junit的使用非常简单,它的基本使用步骤:
- 创建,从junit.framework.TestCase派生unit test需要的test case
- 书写测试方法,提供类似于如下函数签名的测试方法:
public void testXXXXX();
- 编译,书写完test case后,编译所写的test case类
- 运行,启动junit test runner,来运行这个test case。
Junit提供了2个基本的test runner:字符界面和图形界面。启动命令分别如下:
a 图形界面:
java junit.swingui.TestRunner XXXXX
b 字符界面:
java junit.textui.TestRunner XXXXX
2. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
public void testMethod1(){
assertTrue( true);
}
}
3. setUp与tearDown,这两个函数是junit framework中提供初始化和反初始化每个测试方法的。setUp在每个测试方法调用前被调用,负责初始化测试方法所需要的测试环境;tearDown在每个测试方法被调用之后被调用,负责撤销测试环境。它们与测试方法的关系可以描述如下:
测试开始 -> setUp -> testXXXX -> tearDown ->测试结束
4. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
protected void setUp(){
//初始化……
}
public void testMethod1(){
assertTrue( true);
}
potected void tearDown(){
//撤销初始化……
}
}
5. 区分fail、exception。
- fail,期望出现的错误。产生原因:assert函数出错(如assertFalse(true));fail函数产生(如fail(……))。
- exception,不期望出现的错误,属于unit test程序运行时抛出的异常。它和普通代码运行过程中抛出的runtime异常属于一种类型。
对于assert、fail等函数请参见junit的javadoc。
6. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
protected void setUp(){
//初始化……
}
public void testMethod1(){
……
try{
boolean b= ……
assertTrue( b);
throw new Exception( “This is a test.”);
fail( “Unable point.”); //不可能到达
}catch(Exception e){
fail( “Yes, I catch u”); //应该到达点
}
……
}
potected void tearDown(){
//撤销初始化……
}
}
7. 组装TestSuite,运行更多的test。在junit中,Test、TestCase和TestSuite三者组成了composiste pattern。通过组装自己的TestSuite,可以完成对添加到这个TestSuite中的所有的TestCase的调用。而且这些定义的TestSuite还可以组装成更大的TestSuite,这样同时也方便了对于不断增加的TestCase的管理和维护。
它的另一个好处就是,可以从这个TestCase树的任意一个节点(TestSuite或TestCase)开始调用,来完成这个节点以下的所有TestCase的调用。提高了unit test的灵活性。
8. 使用例子:
import junit.framework.Test;
import junit.framework.TestSuite;
public class TestAll{
public class TestAll{
//定义一个suite,对于junit的作用可以视为类似于java应用程序的main。
public static Test suite(){
TestSuite suite = new TestSuite("Running all tests.");
suite.addTestSuite( TestCase1.class);
suite.addTestSuite( TestCase2.class);
return suite;
}
}
运行同运行单独的一个TestCase是一样的,参见step 1 “运行”。
9. 使用Ant junit task。我们除了使用java来直接运行junit之外,我们还可以使用junit提供的junit task与ant结合来运行。涉及的几个主要的ant task如下:
-
-
-
-
-
-
具体的语法请参见相关文档。
10. 使用例子:
<junit printsummary="yes" haltonfailure="no">
<classpath>
<path refid="classpath"/>
<pathelement location="${dist.junit}"/>
</classpath>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<batchtest todir="${doc.junitReport}">
<fileset dir="${dist.junit}" includes="**/*Test.class" />
</batchtest>
TAG: