一个基于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文件:
- /**
- *
- */
- 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。
- 一个基于JUnit的测试框架
- 基于JUnit 3.8 的一个简单的测试程序
- 用JUnit测试Spring管理Hibernate框架的一个例子
- junit--基于测试开发和cobertura框架
- 基于Spring的Junit测试
- 测试junit框架的搭建
- MyEclipse的junit测试框架
- android基础知识12:android自动化测试03—基于junit的android测试框架03
- android自动化测试03—基于junit的android测试框架
- 一个Junit 的测试错误
- 一个简单的Junit测试
- Dbunit(基于junit扩展的数据库测试框架。)vs Jmockit
- 基于spring 3 注解的junit测试
- 基于二分查找的Junit测试实验
- SpringBoot框架下基于Junit的单元测试
- Android Junit测试框架的配置
- JUnit测试框架的使用经验分享
- JUnit测试框架的使用实例
- TOLEContainer部件的用法详解
- Recordset.Open常量速查表
- 向Access插入数据时提示“标准表达式中数据类型不匹配”的解决方法
- 如何使用 ADO FetchProgress 和 FetchComplete 事件
- Pattern-Oriented Software Architecture v1巨详细读书笔记 3
- 一个基于JUnit的测试框架
- Prototype 1.6 边学边译 -- Ajax Ajax.Updater
- oracle 数据库里查看表空间使用状况
- ADO Cancel 方法
- How To Use the ADO FetchProgress and FetchComplete Events
- 再说说SOA...
- 一剂良方克顽疾 英特尔I/O加速技术综述
- 【转】宁做创业狼,不做打工狗(ZT)
- Delphi 常用函数