junit使用简明手册(zz)

 

    在使用XP进行开发的过程,unit test是必不可少的环节。作为unit testjunit是首选的工具。本文从使用目的、如何使用、以及使用中需要考虑的问题,简略描述了junit的基本用法。

使用目的

       junitjava中书写unit testframework,目前一些流行的unit test工具大都都是在junit上扩展而来的。目前它的版本是junit3.8.1,可以从www.junit.org上下载。

 

安装

  1. First, download the latest version of JUnit, referred to below as junit.zip.

  2. Then install JUnit on your platform of choice:

    Windows

    To install JUnit on Windows, follow these steps:

    1. Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.

    2. Add JUnit to the classpath:

      set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%/junit.jar

    Unix (bash)

    To install JUnit on Unix, follow these steps:

    1. Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.

    2. Add JUnit to the classpath:

      export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit.jar


  3. (Optional) Unzip the $JUNIT_HOME/src.jar file.

  4. 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.

  5. 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.       setUptearDown,这两个函数是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.       区分failexception

-          fail,期望出现的错误。产生原因:assert函数出错(如assertFalse(true));fail函数产生(如fail(……))。

-          exception,不期望出现的错误,属于unit test程序运行时抛出的异常。它和普通代码运行过程中抛出的runtime异常属于一种类型。

对于assertfail等函数请参见junitjavadoc

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中,TestTestCaseTestSuite三者组成了composiste pattern。通过组装自己的TestSuite,可以完成对添加到这个TestSuite中的所有的TestCase的调用。而且这些定义的TestSuite还可以组装成更大的TestSuite,这样同时也方便了对于不断增加的TestCase的管理和维护。

       它的另一个好处就是,可以从这个TestCase树的任意一个节点(TestSuiteTestCase)开始调用,来完成这个节点以下的所有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 taskant结合来运行。涉及的几个主要的ant task如下:

-          ,定义一个junit task

-          ,位于 中,运行多个TestCase

-          ,位于 中,运行单个TestCase

-          ,位于 中,定义一个测试结果输出格式

-          ,定义一个junitreport task

-          ,位于 中,输出一个junit report

具体的语法请参见相关文档。

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>

</junit>

 

<junitreport todir="${doc.junitReport}">

    <fileset dir="${doc.junitReport}">

        <include name="TEST*-*.xml"/>

    </fileset>

    <report format="frames" styledir="${junit.styleDir}" todir="${doc.junitReport}"/>

</junitreport>

检查表

       junit的使用并不很难,然而要书写一个好的TestCase却并非易事。一个不好的TestCase往往是既浪费了时间,也起不了实际的作用。相反,一个好的TestCase,不仅可以很好的指出代码中存在的问题,而且也可以作为代码更准确的文档,同时还在持续集成的过程中起非常重要的作用。在此给出书写TestCase时需要注意的几点:

-          测试的独立性:一次只测试一个对象,方便定位出错的位置。这有2层意思:一个TestCase,只测试一个对象;一个TestMethod,只测试这个对象中的一个方法。

-          给测试方法一个合适的名字

-          assert函数中给出失败的原因,如:assertTrue( “… should be true”,  ……),方便查错。在这个例子中,如果无法通过assertTrue,那么给出的消息将被显示。在junit中每个assert函数都有第一个参数是出错时显示消息的函数原型。

-          测试所有可能引起失败的地方,如:一个类中频繁改动的函数。对于那些仅仅只含有getter/setter的类,如果是由IDE(如Eclipse)产生的,则可不测;如果是人工写,那么最好测试一下。

-          setUptearDown中的代码不应该是与测试方法相关的,而应该是全局相关的。如针对与测试方法AB,在setUptearDown中的代码应该是AB都需要的代码。

-          测试代码的组织:相同的包,不同的目录。这样,测试代码可以访问被测试类的protected变量/方法,方便测试代码的编写。放在不同的目录,则方便了测试代码的管理以及代码的打包和发布。一个例子如下:

src   <=源代码根目录

 |---com

     |---mod1

         |---class1

junit   <=测试代码根目录

 |---com

     |---mod1

         |---class1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值