`

VB6-改造ComUnit(免除用例名称注册)

阅读更多

在进行测试驱动开发的过程中,编写测试用例是比较繁琐的,但是更繁琐的是使用ComUnit框架进行测试的过程中,需要在频繁进行测试用例名称的注册,如下边的代码:

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

这很好,但是测试用例的名称的注册、删除、修改的过程就麻烦了,需要改好多地方,时间长了,Array就好长的,周末在回家的路上又开始翻看《重构》这本书, Martin Fowler提到,在Junit框架中,如果测试用例的名称如果是固定前缀的,那么,就不需要频繁注册测试用例名称了,嗯,这个方法很好,至少我这个懒人很喜欢,经过研究,利用TLI很容易就实现了在VB6中得这种反射用法,代码如下:
' 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()
    ITestContainer_TestCaseNames = AutoGetTestCaseNames
End Property

'Auto get testcasenames
'
TestCaseName prefix must is "Test_"
Private Function AutoGetTestCaseNames() As Variant()
       
Dim TypeLib As Object
       
Dim Prop   As Object
       
Dim i As Integer
       
Dim vHas As Boolean
       
Dim v() As Variant
       
Set TypeLib = CreateObject("TLI.TLIApplication")
       
Set TypeLib = TypeLib.InterfaceInfoFromObject(Me)
       
For Each Prop In TypeLib.Members
            
If Prop.InvokeKind = INVOKE_FUNC Then
                
If UCase(VBA.Left(Prop.Name, 5)) = UCase("Test_"Then
                    
If vHas = False Then
                        vHas 
= True
                        
ReDim Preserve v(0)
                    
Else
                        
ReDim Preserve v(UBound(v) + 1)
                    
End If
                    v(
UBound(v)) = Prop.Name
                
End If
            
End If
       
Next
       
Set TypeLib = Nothing
       
Set Prop = Nothing
       
On Error GoTo onErrors:
       AutoGetTestCaseNames 
= v
       
Exit Function
onErrors:
       AutoGetTestCaseNames 
= Array()
End Function

测试用例如下:
    Public Sub Test_Ver(oTestResult As TestResult)
         
With oTestResult
            f.Config App.Path 
& "\TestMatter\FileClass\ver.exe"
            .Assert f.Ver() 
= "2.0.0.30""0001"
            f.Config App.Path 
& "\TestMatter\FileClass\ver.txt"
            
On Error GoTo onErrors
            f.Ver
            .Assert 
False"0002"
onErrors:
         
End With
    
End Sub


利用这段代码所有Test_开头得测试用例,系统会自动发现,就不需要频繁注册用例名称了,感觉爽。

现在才感受到开源之美,很多小功能,自己修订就好了,而且用起来也好舒服。

点滴:

以前也看过几遍Refactoring这本书了,每次都有新得收获,最近采用ComUnit进行开发后,效率真的不错,而且开发方式也有了本质得变化,测试驱动开发必须经过实践,才能感受到其中得乐趣。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics