VB单元测试

         转自:软件测试网
  vb有多个单元测试的软件,如VBunit,communit。此处选择communit来作为单元测试的,原因是一、它是完全开放源码的,二、它不仅能 测试VB源代码,还能测试标准COM组件。communit可以从sourceforge.net下载,也可以 本地下载,本地版本与有两个修改,一是修正模板的安装,原来的包没有完全设置好,二是增加了时间的计算,能显示测试花费的时间。

    使用,把包下载,然后解到一个目录,运行install.exe,打开VB。可以发现可以新建CoMunit Test Project的工程。有两个文件frmTestRunner的form,TCTestContainer是测试类。下面我们看一下如何使用 comunit。先下载测试代码

     这个代码是测试二进制文件在数据库里的存取。有关二进制代码在数据库里存取的技术问题,请看另一篇文章。用什么办法来确定代码是正确工作了呢?答案当然是单元测试。

    测试代码很简单,但的确很有效,它保证这段代码是正确的工作了。

'  COMUnit 1.1 - TestContainer Class

Option   Explicit

'  Interface declaration
Implements  ITestContainer
' 定义一个recordset
Dim  k  As   New  adodb.Recordset
' 定义bin类
Dim  BinTest  As  clsManagerBinFields
' 定义源文件名和目标文件名
Dim  SourceFile  As   String , DesFile  As   String
'  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( " TestFields " )
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
'
指定源文件名,这里就指定这个文件
SourceFile  =  App.Path  &   " \tctestcontainer.cls "
' 指定目标文件名,这里指定为当前目录下的test
 DesFile  =  App.Path  &   " \test "

 
Set  BinTest  =   New  clsManagerBinFields

' 如果目标文件存在,则删除这个文件
  If   Dir (DesFile)  <>   ""   Then   Kill  DesFile

' 建立一个新的Recordset,这里用一个虚拟的数据源来代替,这样就不用打开数据库了
 k.Source  =   " test "
 k.Fields.Append 
" thefile " , adBinary,  - 1 , adFldUpdatable
 k.Open , , adOpenKeyset, adLockBatchOptimistic
 k.AddNew

End Sub

' Destroy the test fixture
Public   Sub  ITestContainer_TearDown()
'  TODO: destruct your test fixture here
'
 k.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\xpconnect\db\test.mdb;Persist Security Info=False"
'
 k.UpdateBatch adAffectAllChapters
'
 k.Close
'
测试完成后自动完成
  Set  k  =   Nothing
 
Set  BinTest  =   Nothing

End Sub

' Public Sub testSampleMethod(oTestResult As TestResult)
'
 TODO: add your test code here
'
End Sub
Private   Sub  TestFieldtoFile(oTestResult  As  TestResult)
 oTestResult.Assert BinTest.FieldToFile(DesFile, k!thefile), 
" 导出到文件不成功 "
End Sub
Private   Sub  TestFiletofield(oTestResult  As  TestResult)
 oTestResult.Assert BinTest.fileTofield(SourceFile, k!thefile), 
" 导入到数据库不成功 "
End Sub
Public   Sub  TestFields(oTestResult  As  TestResult)
 TestFiletofield oTestResult
 TestFieldtoFile oTestResult
 oTestResult.Assert 
FileLen (SourceFile)  =   FileLen (DesFile),  " 文件不相符! "
End Sub

写单元测试代码很简单,只要完成几个函数就可以了。

 
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( " TestFields " )
End Property

第一个,ITestContainer_TestCaseNames。是测试函数的列表, ITestContainer_TestCaseNames = Array("TestFields")TestFields就是函数名,要测试这个函数,就写在这个数组里。
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

这个函数是自动生成的,不用修改。 
' 在测试开始时自动调用
Public   Sub  ITestContainer_Setup()

End Sub

这个函数是每一个测试函数运行时自动调用的,可以在这个函数中初始化测试的内容。
Public   Sub  ITestContainer_TearDown()

End Sub
  这个函数是每个测试函数结束时,自动运行,在这个函数中可以释放在测试中使用的资源。
' Public Sub testSampleMethod(oTestResult As TestResult)
'
 TODO: add your test code here
'
End Sub
Private   Sub  TestFieldtoFile(oTestResult  As  TestResult)
 oTestResult.Assert BinTest.FieldToFile(DesFile, k!thefile), 
" 导出到文件不成功 "
End Sub
Private   Sub  TestFiletofield(oTestResult  As  TestResult)
 oTestResult.Assert BinTest.fileTofield(SourceFile, k!thefile), 
" 导入到数据库不成功 "
End Sub
Public   Sub  TestFields(oTestResult  As  TestResult)
 TestFiletofield oTestResult
 TestFieldtoFile oTestResult
 oTestResult.Assert 
FileLen (SourceFile)  =   FileLen (DesFile),  " 文件不相符! "
End Sub

这三个函数就是测试函数。TestFields是公共测试函数,另两个函数由TestFields调用。先把文件导入到数据库,再从数据库把文件导出到文件中,最后比较两个文件是否相同。

下面我们来看怎么在VB中使用。打开工程,在VB工具菜单中选项中把修改错误捕获的设置。改成遇到未处理错误的中断。如下图所未。

1306160.jpg

运行程序,出现下面这个界面。下面可以看到有一个类可以测试,选择这个类,看Test Case列表,就能看到TestFields这个测试函数了。 按RunTest。如果通过测试,就能说明这两段代码就达成目的了。

1306161.jpg

下面我们来看出现错误的情况。把函数TestField中TestFieldtoFile oTestResult注释掉。这样就只导入文件,不进行导出文件。看一下Run Tests的结果,如下图所未:

1306162.jpg

可以看到,progress条变成红色,表示运行过程中有出错的测试。下错误列表中列出出现错误函数名及错误内容,可以双击出现某条单独的错误说明。下面状态条有测试的统计信息。

我们看上面的简单的39行代码,就能保证另一个类代码的正确性,并且,以后可以进行任意的修改,只要代码能够通过测试就能保证修改的没有产生意外的错误。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值