junit_1

来源:互联网 发布:淘宝店铺特效css代码 编辑:程序博客网 时间:2024/06/06 16:28

 单元测试的目的是为了证明结果与期望是否一致。

 1.使用Junit的最佳实践:

      (1)新建一个source folder 类型的文件夹,用于存放测试代码

      (2)测试类的包名要与源文件的包名保持一致,这样可以省去很多麻烦。

      (3)测试类的命名规则:以Test开头,或者以Test结尾,比如,源文件为Person,则测试类命名为TestPerson或者PersonTest

      (4)测试类必须继承于TestCase

2.在编写测试用例【测试方法】时要牢记以下原则:

       (1)方法是public的

       (2)方法是void的

       (3)方法是没有参数的

        (4)方法必须以test开头。

   注意:Test    Case之间必须保持完全的独立性,不允许出现任何的依赖关系。

3.TestCase类中的两个方法:

 

protected  void

setUp()
          Sets up the fixture, for example, open a network connection.This method is called before a test is executed.

protected  void

tearDown()
          Tears down the fixture, for example, close a network connection.This method is called after a test is executed。

 

可以看出,两个方法的执行顺序,setUp是在每一个测试用例执行前执行,tearDown是在每一个测试用例执行后执行。利用这个特性可以进行代码的重构等。

setUp------》测试用例-----》tearDown

 

4.测试用例的执行

     (1)没有main函数:使用myeclipse集成的junit插件

      (2)编写main函数:

                  *使用文本命令行:junit.textui.TestRunner.run(测试类.class);

                  *使用AWT图形界面:junit.awtui.TestRunner.run(测试类.class);

                  *使用Swing图像界面:junit.swingui.TestRunner.run(测试类.class);

原创粉丝点击