VB6-改造ComUnit(免除用例名称注册)
2005-10-24 12:04
309 查看
在进行测试驱动开发的过程中,编写测试用例是比较繁琐的,但是更繁琐的是使用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进行开发后,效率真的不错,而且开发方式也有了本质得变化,测试驱动开发必须经过实践,才能感受到其中得乐趣。
' 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进行开发后,效率真的不错,而且开发方式也有了本质得变化,测试驱动开发必须经过实践,才能感受到其中得乐趣。
相关文章推荐
- VB6-单元测试工具ComUnit
- vb6内联汇编,调用函数指针,不注册调用com
- 10月26日练习注册用例的编写与ibatis培训
- 命名空间“Microsoft.Office.Interop”中不存在类型或命名空间名称“Owc11”。以及检索 COM 类工厂中 CLSID 为 {0002E55D-0000-0000-C000-00
- Google 在情人节开放Gmail注册--(转自Gfans.com)
- thrift服务化改造——注册、发现
- 2010年即将到来,用我的名字注册了的新域名 TerryFeng.com
- selenium---unittest框架测试用例函数执行顺序 优先级
- Unittest操作简单实例进行系统讲解测试用例
- VS2005 setup 工程 注册com
- 做微信支付时出现"当前页面的URL未注册:http://www.xxx.com/`````````"
- vfp:获取com对象的注册信息
- COM 组件设计与应用(七)---编译、注册、调用
- (selenium 六)unittest通过测试套件组织用例 (2)
- 2、编写单元测试用例,对用户注册功能的DAO层进行测试。(注意:测试用例应考虑成功和失败的情况)
- 如何编译免注册COM(reg-free COM)
- MFC程序运行时注册自身所需要的COM组件
- 关于VC向导生成的COM的注册与反注册
- 症状解决,原因不详的用非默认管理权限账户登录COM注册成功但找不到类型问题
- 使用VB6创建COM加载宏