VB6-单元测试工具ComUnit

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

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

None.gif '  COMUnit 1.1 - TestRunner form
None.gif

None.gif
Option   Explicit
None.gif
None.gif
'  Initialize the TestRunner control
None.gif
Private   Sub  Form_Load()
None.gif    
'  TODO: add instances of your TestContainer classes to the UnitRunner control
None.gif
     '  e.g. UnitRunner1.AddTestContainer New TCTestContainer
None.gif
     ' ************************************************
None.gif
     ' 必须向UnitRunner添加一个控制对象,别的不需要修改
None.gif
    UnitRunner1.AddTestContainer  New  TCTestContainer
None.gif    
' ************************************************
None.gif
End Sub
None.gif
None.gif
'  Run the tests selected in the UnitRunner
None.gif
Private   Sub  btnRun_Click()
None.gif    UnitRunner1.Run
None.gif
End Sub
None.gif
None.gif
'  Close the form
None.gif
Private   Sub  btnClose_Click()
None.gif    Unload Me
None.gif
End Sub
None.gif
None.gif
'  Resize the UnitRunner control and the buttons on the form
None.gif
Private   Sub  Form_Resize()
None.gif    UnitRunner1.Move 
0 0 , ScaleWidth, PosInt(ScaleHeight  -  btnClose.Height  -   50 )
None.gif    btnClose.Move PosInt(ScaleWidth 
-  btnClose.Width), PosInt(ScaleHeight  -  btnClose.Height)
None.gif    btnRun.Move PosInt(ScaleWidth 
-  btnClose.Width  -  btnRun.Width  -   100 ), PosInt(ScaleHeight  -  btnRun.Height)
None.gif
End Sub
None.gif
None.gif
Private   Function  PosInt(iValue)  As   Integer
None.gif    PosInt 
=  IIf(iValue  >   0 , iValue,  0 )
None.gif
End Function
None.gif
None.gif '  COMUnit 1.1 - TestContainer Class
None.gif'
建议类的前缀为TC
None.gif
Option   Explicit
None.gif
None.gif
'  Interface declaration
None.gif
Implements ITestContainer
None.gif
None.gif
'  Fixture Member Variables
None.gif'
 TODO: specify your TestContainer test fixture member variables here
None.gif

None.gif
'  Return the name of the different test case methods in this test container
None.gif
Public   Property   Get  ITestContainer_TestCaseNames()  As  Variant()
None.gif    
'  TODO: add the names of your test methods as a parameter into the Array() function
None.gif
     ' 必须将测试方法的名字加到数组中
None.gif
    ITestContainer_TestCaseNames  =   Array ( " TestString " " TestLong " )
None.gif
End Property
None.gif
None.gif
'  Run the specified test case methods in this test container
None.gif
Public   Sub  ITestContainer_RunTestCase(oTestCase  As  ITestCase, oTestResult  As  TestResult)
None.gif    
On   Error   GoTo  ErrorHandler
None.gif    InvokeHook Me, oTestCase.Name, INVOKE_FUNC, oTestResult
None.gif    
'  CallByName Me, oTestCase.Name, VbMethod, oTestResult
None.gif
     Exit   Sub
None.gifErrorHandler:
None.gif    oTestResult.AddError Err.Number, Err.Source, Err.Description
None.gif
End Sub
None.gif
None.gif
' Initialize the test fixture
None.gif
Public   Sub  ITestContainer_Setup()
None.gif    
'  TODO: initialize your test fixture here
None.gif
End Sub
None.gif
None.gif
' Destroy the test fixture
None.gif
Public   Sub  ITestContainer_TearDown()
None.gif    
'  TODO: destruct your test fixture here
None.gif
End Sub
None.gif
None.gif
' Public Sub testSampleMethod(oTestResult As TestResult)
None.gif
     '  TODO: add your test code here
None.gif'
End Sub
None.gif

None.gif
Public   Sub  TestString(oTestResult  As  TestResult)
None.gif    oTestResult.Assert 
" String "   =   " String " " 字符串不相等 "
None.gif
End Sub
None.gif
None.gif
Public   Sub  TestLong(oTestResult  As  TestResult)
None.gif    oTestResult.AssertEqualsLong 
1 456 " 数值不相等 "
None.gif
End Sub
None.gif

在窗体中要将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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值