注意:本文出自 “阿飞的专栏” 博客,如果要转载本文章,请与作者联系!
并注明来源: 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文件:
- /**
- *
- */
- package org.system.test.demo;
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.gos.system.hpcg.test.demo.util.LoginInfo;
- /**
- * @author chenfei
- *
- */
- public class FunATest extends TestCase {
- /**
- * . LoginInfo
- */
- private static LoginInfo login;
- /**
- * the log object.
- */
- private static Log log = LogFactory.getLog(FunATest.class);
- public static void main(String args[]) {
- //在命令行中也可以对FunA进行集成测试
- junit.textui.TestRunner.run(FunATest.suite());
- }
- /**
- * Construct function.
- */
- public FunATest(String name) {
- super(name);
- }
- /**
- * to determine which functions should be test. Don't change the order of
- * test functions.
- *
- * @return the Test object.
- */
- public static Test suite() {
- //这里是FunA的集成测试,必须将需要进行单元测试的方法加入到这里来!
- TestSuite suite = new TestSuite();
- suite.addTest(new FunATest("testA1"));
- suite.addTest(new FunATest("testA2"));
- suite.addTest(new FunATest("testA3"));
- suite.addTest(new FunATest("testA4"));
- return suite;
- }
- public static void testA1(){
- //这里可以对testA1方法进行测试
- System.out.println("In FunA.testA1()");
- }
- public static void testA2() {
- //这里可以对testA2方法进行测试
- System.out.println("In FunA.testA2()");
- }
- public static void testA3() {
- //这里可以对testA3方法进行测试
- System.out.println("In FunA.testA3()");
- }
- public static void testA4() {
- //这里可以对testA4方法进行测试
- System.out.println("In FunA.testA4()");
- }
- }
FuncBTest.java文件同FuncATest.java文件,在此省略。
DemoTest.java 文件:
- package org.system.test.demo;
- /**
- *
- */
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.gos.system.hpcg.test.demo.util.Login;
- import org.gos.system.hpcg.test.demo.util.LoginInfo;
- /**
- * @author chenfei
- *
- */
- public class DemoTest extends TestCase {
- /**
- * . LoginInfo
- */
- private static LoginInfo login;
- /**
- * the log object.
- */
- private static Log log = LogFactory.getLog(DemoTest.class);
- public static void main(String args[]) {
- junit.textui.TestRunner.run(DemoTest.suite());
- }
- public DemoTest(String name) {
- super(name);
- }
- /**
- * to determine which functions should be test. Don't change the order of
- * test functions.
- *
- * @return the Test object.
- */
- public static Test suite() {
- TestSuite suite = new TestSuite();
- //suite.addTest(new DemoTest("login"));
- suite.addTest(new DemoTest("init"));
- suite.addTest(new DemoTest("call"));
- suite.addTest(new DemoTest("destroy"));
- return suite;
- }
- /**
- * user login.
- */
- public static void login() throws Exception {
- login = Login.getInstance();
- }
- public static void init() {
- try {
- login();
- } catch (Exception e) {
- System.out.println(e.getLocalizedMessage());
- log.error(e.getLocalizedMessage());
- }
- //your init contents
- }
- public static void call() {
- //your concreate function testing contents
- junit.textui.TestRunner.run(FunATest.suite());
- junit.textui.TestRunner.run(FunBTest.suite());
- }
- public static void destroy() {
- //your destroy contents
- }
- }
DemoTest.java 文件就不多注释了,具体与FuncATest.java文件类似。
另外,
Login.java ——登陆验证类
LoginInfo.java ——用户信息的Javabean类
这两个文件主要是根据自己的情况而定,可有可无,因此代码也不再列举出来。
好了,我们可以看到,单独做单元测试的时候呢,我们可以独立的运行FuncATest.java即可,需要对整个系统进行集成测试呢,就运行DemoTest.java ,可分可合,灵活方便,尤其是对比较大的系统的时候这个测试框架就更有用了,大家可以先做好自身模块的单元测试和集成,然后PM就可以很方便的将各测试集成起来run。