' COMUnit 1.1 - TestRunner form OptionExplicit ' Initialize the TestRunner control PrivateSub 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 PrivateSub btnRun_Click() UnitRunner1.Run End Sub ' Close the form PrivateSub btnClose_Click() Unload Me End Sub ' Resize the UnitRunner control and the buttons on the form PrivateSub 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 PrivateFunction PosInt(iValue) AsInteger PosInt = IIf(iValue >0, iValue, 0) End Function
' COMUnit 1.1 - TestContainer Class '建议类的前缀为TC OptionExplicit ' 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 PublicPropertyGet 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 PublicSub ITestContainer_RunTestCase(oTestCase As ITestCase, oTestResult As TestResult) OnErrorGoTo ErrorHandler InvokeHook Me, oTestCase.Name, INVOKE_FUNC, oTestResult ' CallByName Me, oTestCase.Name, VbMethod, oTestResult ExitSub ErrorHandler: oTestResult.AddError Err.Number, Err.Source, Err.Description End Sub 'Initialize the test fixture PublicSub ITestContainer_Setup() ' TODO: initialize your test fixture here End Sub 'Destroy the test fixture PublicSub ITestContainer_TearDown() ' TODO: destruct your test fixture here End Sub 'Public Sub testSampleMethod(oTestResult As TestResult) ' TODO: add your test code here 'End Sub PublicSub TestString(oTestResult As TestResult) oTestResult.Assert "String"="String", "字符串不相等" End Sub PublicSub TestLong(oTestResult As TestResult) oTestResult.AssertEqualsLong 1, 456, "数值不相等" End Sub
在窗体中要将Runner和类进行绑定,建议的类名前缀为TC,新增的测试函数一定要在ITestContainer_TestCaseNames中进行注册,新增的测试函数一定要有oTestResult As TestResult这个声明,而且只能有这么一个声明。