51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 5049|回复: 0
打印 上一主题 下一主题

VB6-单元测试工具ComUnit

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2006-11-12 20:22:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
http://duiker.cnblogs.com/archive/2005/10/18/256940.html


在使用过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 0, 0, 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 1, 456, "数值不相等"
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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-16 14:40 , Processed in 0.067243 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表