VB6-单元测试工具ComUnit

在使用过NUnit后,一直想在VB6中使用一下单元测试工具,目前比较流行的是VBUnit和ComUnit,我比较喜欢ComUnit主要是它是开源的,当然用起来比较爽了,可以在http://comunit.sourceforge.net/进行下载。

ComUnit使用比较简单,注意下边几点后就可以使用了。

' COMUnit 1.1 - TestRunner form

Option Explicit

' Initialize the TestRunner control
Private Sub Form_Load()
    
' TODO: add instances of your TestContainer classes to the UnitRunner control
    ' e.g. UnitRunner1.AddTestContainer New TCTestContainer
    '************************************************
    '必须向UnitRunner添加一个控制对象,别的不需要修改
    UnitRunner1.AddTestContainer New TCTestContainer
    
'************************************************
End Sub

' Run the tests selected in the UnitRunner
Private Sub btnRun_Click()
    UnitRunner1.Run
End Sub

' Close the form
Private Sub btnClose_Click()
    Unload Me
End Sub

' Resize the UnitRunner control and the buttons on the form
Private Sub Form_Resize()
    UnitRunner1.Move 
00, ScaleWidth, PosInt(ScaleHeight - btnClose.Height - 50)
    btnClose.Move PosInt(ScaleWidth 
- btnClose.Width), PosInt(ScaleHeight - btnClose.Height)
    btnRun.Move PosInt(ScaleWidth 
- btnClose.Width - btnRun.Width - 100), PosInt(ScaleHeight - btnRun.Height)
End Sub

Private Function PosInt(iValue) As Integer
    PosInt 
= IIf(iValue > 0, iValue, 0)
End Function
' COMUnit 1.1 - TestContainer Class
'
建议类的前缀为TC
Option Explicit

' Interface declaration
Implements ITestContainer

' Fixture Member Variables
'
 TODO: specify your TestContainer test fixture member variables here

' Return the name of the different test case methods in this test container
Public Property Get ITestContainer_TestCaseNames() As Variant()
    
' TODO: add the names of your test methods as a parameter into the Array() function
    '必须将测试方法的名字加到数组中
    ITestContainer_TestCaseNames = Array("TestString""TestLong")
End Property

' Run the specified test case methods in this test container
Public Sub ITestContainer_RunTestCase(oTestCase As ITestCase, oTestResult As TestResult)
    
On Error GoTo ErrorHandler
    InvokeHook Me, oTestCase.Name, INVOKE_FUNC, oTestResult
    
' CallByName Me, oTestCase.Name, VbMethod, oTestResult
    Exit Sub
ErrorHandler:
    oTestResult.AddError Err.Number, Err.Source, Err.Description
End Sub

'Initialize the test fixture
Public Sub ITestContainer_Setup()
    
' TODO: initialize your test fixture here
End Sub

'Destroy the test fixture
Public Sub ITestContainer_TearDown()
    
' TODO: destruct your test fixture here
End Sub

'Public Sub testSampleMethod(oTestResult As TestResult)
    ' TODO: add your test code here
'
End Sub

Public Sub TestString(oTestResult As TestResult)
    oTestResult.Assert 
"String" = "String""字符串不相等"
End Sub

Public Sub TestLong(oTestResult As TestResult)
    oTestResult.AssertEqualsLong 
1456"数值不相等"
End Sub

在窗体中要将Runner和类进行绑定,建议的类名前缀为TC,新增的测试函数一定要在ITestContainer_TestCaseNames中进行注册,新增的测试函数一定要有oTestResult As TestResult这个声明,而且只能有这么一个声明。

还有就是要在IDE的选项中,将错误捕获改为“遇到未处理的错误时中断”就可以享受VB6下边的单元测试了。

参考文章:
http://champion.ewuxi.com/old/XP/xppractice/vbunit.htm
http://www.soho-works.net/BLOG/313.asp
http://comunit.sourceforge.net/?page=tutorial.html

posted on 2005-10-18 10:03 Duiker 阅读(931) 评论(0)  编辑 收藏 所属分类: 技术


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接:
 



导航

公告


效率狂,质量狂,简简单单,享受生活,让代码充满阳光,让使用者感到快乐,把软件开发看作是一种沟通游戏。

message


bookmark


搜索

 

最新随笔

最新评论

  • 1. re: 只有分享才能一起进步
  • 5、简单要讲究方法和实际,不能跟风。
    非常同意这点!
  • --wensanlu
  • 2. re: 数据库设计经验谈(转)
  • 还是有些东西说的比较好!
  • --liffos.fang
  • 3. re: 快变
  • 梅姐,要是不比上学时候成熟,那就成精了,呵呵。
  • --Duiker
  • 4. re: 快变
  • 感觉你的博客不错,文笔说不上很精细,但是字里行间都是真情实感,看着让人感觉你的生活很充实,拥有这样的人生,也是一件很美的事情!既然活着就要活出个样来.感觉你比上学时成熟多啦!嘿嘿/1
  • --梅姐
  • 5. re: 快变
  • 那个秋王,死前简直就无敌,后来活了,跟普通人肉搏都那么菜
  • --丁学