一个基于JUnit的测试框架

来源:互联网 发布:2017php免杀一句话 编辑:程序博客网 时间:2024/06/05 12:45

注意:本文出自 “阿飞的专栏” 博客,如果要转载本文章,请与作者联系!

并注明来源: http://blog.csdn.net/faye0412/archive/2008/12/31/3663292.aspx

这个测试框架是我在做一个比较大的工程时写的一个框架,专门对工程进行单元测试和集成测试的,这个框架的功能和结构都很简单,在这里分享给大家,也希望大家能多提点宝贵的意见和建议,谢谢!

一、工程的结构如下:

-test

  --org.system.test.demo

     ----DemoTest.java

     ----FuncATest.java

     ----FuncBTest.java

   --org.system.test.util

     ----Login.java

     ----LoginInfo.java

 

其中:

org.system.test.demo为单元测试和集成测试的java类的包,org.system.test.util各工具类的包(这里用用户登陆的验证作为演示)。

 

DemoTest.java  ——为集成测试类

FuncATest.java ——为FuncA单元测试类

FuncBTest.java ——为FuncB单元测试类

Login.java  ——登陆验证类

LoginInfo.java  ——用户信息的Javabean类

 

二、详细代码

FuncATest.java文件:

  1. /**
  2.  * 
  3.  */
  4. package org.system.test.demo;
  5. import junit.framework.Test;
  6. import junit.framework.TestCase;
  7. import junit.framework.TestSuite;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.gos.system.hpcg.test.demo.util.LoginInfo;
  11. /**
  12.  * @author chenfei
  13.  *
  14.  */
  15. public class FunATest extends TestCase {
  16.     /**
  17.      * . LoginInfo
  18.      */
  19.     private static LoginInfo login;
  20.     /**
  21.      * the log object.
  22.      */
  23.     private static Log log = LogFactory.getLog(FunATest.class);
  24.     public static void main(String args[]) {
  25.                 //在命令行中也可以对FunA进行集成测试
  26.         junit.textui.TestRunner.run(FunATest.suite());
  27.     }
  28.     /**
  29.      * Construct function.
  30.      */
  31.     public FunATest(String name) {
  32.         super(name);
  33.     }
  34.     /**
  35.      * to determine which functions should be test. Don't change the order of
  36.      * test functions.
  37.      * 
  38.      * @return the Test object.
  39.      */
  40.     public static Test suite() {
  41.                 //这里是FunA的集成测试,必须将需要进行单元测试的方法加入到这里来!
  42.         TestSuite suite = new TestSuite();
  43.         suite.addTest(new FunATest("testA1"));
  44.         suite.addTest(new FunATest("testA2"));
  45.         suite.addTest(new FunATest("testA3"));
  46.         suite.addTest(new FunATest("testA4"));
  47.         return suite;
  48.     }
  49.     public static void testA1(){
  50.                 //这里可以对testA1方法进行测试
  51.         System.out.println("In FunA.testA1()");
  52.     }
  53.     public static void testA2() {
  54.                 //这里可以对testA2方法进行测试
  55.         System.out.println("In FunA.testA2()");
  56.     }
  57.     public static void testA3() {
  58.                 //这里可以对testA3方法进行测试
  59.         System.out.println("In FunA.testA3()");
  60.     }
  61.     public static void testA4() {
  62.                 //这里可以对testA4方法进行测试
  63.         System.out.println("In FunA.testA4()");
  64.     }
  65. }

FuncBTest.java文件同FuncATest.java文件,在此省略。

DemoTest.java 文件:

  1. package org.system.test.demo;
  2. /**
  3.  * 
  4.  */
  5. import junit.framework.Test;
  6. import junit.framework.TestCase;
  7. import junit.framework.TestSuite;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.gos.system.hpcg.test.demo.util.Login;
  11. import org.gos.system.hpcg.test.demo.util.LoginInfo;
  12. /**
  13.  * @author chenfei
  14.  * 
  15.  */
  16. public class DemoTest extends TestCase {
  17.     /**
  18.      * . LoginInfo
  19.      */
  20.     private static LoginInfo login;
  21.     /**
  22.      * the log object.
  23.      */
  24.     private static Log log = LogFactory.getLog(DemoTest.class);
  25.     public static void main(String args[]) {
  26.         junit.textui.TestRunner.run(DemoTest.suite());
  27.     }
  28.     public DemoTest(String name) {
  29.         super(name);
  30.     }
  31.     /**
  32.      * to determine which functions should be test. Don't change the order of
  33.      * test functions.
  34.      * 
  35.      * @return the Test object.
  36.      */
  37.     public static Test suite() {
  38.         TestSuite suite = new TestSuite();
  39.         //suite.addTest(new DemoTest("login"));
  40.         suite.addTest(new DemoTest("init"));
  41.         suite.addTest(new DemoTest("call"));
  42.         suite.addTest(new DemoTest("destroy"));
  43.         return suite;
  44.     }
  45.     /**
  46.      * user login.
  47.      */
  48.     public static void login() throws Exception {
  49.         login = Login.getInstance();
  50.     }
  51.     public static void init() {
  52.         try {
  53.             login();
  54.         } catch (Exception e) {
  55.             System.out.println(e.getLocalizedMessage());
  56.             log.error(e.getLocalizedMessage());
  57.         }
  58.         
  59.         //your init contents
  60.     }
  61.     public static void call() {
  62.         //your concreate function testing contents
  63.         junit.textui.TestRunner.run(FunATest.suite());
  64.         junit.textui.TestRunner.run(FunBTest.suite());
  65.     }
  66.     public static void destroy() {
  67.         //your destroy contents
  68.     }
  69. }

DemoTest.java 文件就不多注释了,具体与FuncATest.java文件类似。

另外,

Login.java  ——登陆验证类

LoginInfo.java  ——用户信息的Javabean类

这两个文件主要是根据自己的情况而定,可有可无,因此代码也不再列举出来。

 

好了,我们可以看到,单独做单元测试的时候呢,我们可以独立的运行FuncATest.java即可,需要对整个系统进行集成测试呢,就运行DemoTest.java ,可分可合,灵活方便,尤其是对比较大的系统的时候这个测试框架就更有用了,大家可以先做好自身模块的单元测试和集成,然后PM就可以很方便的将各测试集成起来run。