COMUnit Tutorial
This page describes how to get started building your own unit tests with COMUnit.Create a new unit test project in Visual Basic
New: In COMUnit 1.1 it is now easier than ever to create new COMUnit Test Projects. If you chose to install COMUnit Templates during the COMUnit installation (this is the default option), you can create a new COMUnit project simply by opening a new Visual Basic project and selecting COMUnit Test Project from the New Project window. Doing this will create a new project that will contain the following files:- COMUnitTemplates project: rename this project to describe the unit (component) you will be developing tests for.
- frmTestRunner form: this form contains the UnitRunner control and 2 buttons. This form will be displayed when the project is executed.
- TCTestContainer class: this class is a sample TestContainer class. Add all of you test code in here or create new COMUnit TestCase classes as necessary. Rename this class to suit your purposes. Typically it will be named after a class in the component that you will be testing. We recommend using the prefix 'TC' to identify the TestContainer classes.
Building Your Own Unit Tests
You've set up the VB project and the code runs, so now what? What does all that skeleton code mean anyway? First of all, I should explain a bit more about the two files that are currently included in your test project. Then I'll give you an idea of how you can go about adding your own unit tests methods.frmUnitRunner Form
The test project form is used to contain the ActiveX test runner controls. This form serves as the startup object for the test project.The UnitRunner Form contains the COMUnitRunner control. The COMUnitRunner control is used to select which tests to run. Depending on the user's selection, it builds a TestSuite containing the selected TestContainers and TestMethods. The COMUnitRunner control also provides the facility to execute the tests in the generated TestSuite.
If you examine the code for the Form, you will notice two things:
- The TestContainer classes contained within the test project are added to the UnitRunner control in the Form_Load method. If you create a new TestContainer class for the test project ensure that you add an instance of the class to the UnitRunner using the AddTestContainer method. This way the test methods in your TestContainer class will be available to the UnitRunner control.
- Clicking on the Run Tests button on the form will instruct the UnitRunner control to build a TestSuite using the selected unit test(s) and then execute them.
TCTestContainer Class
The TestContainer class (ie. TCTestContainer) is the container for the unit test code. The test project will typically contain one TestContainer class for each public class in the component under test. All test classes will have the following characteristics:- They must implement the ITestContainer interface.
- The ITestContainer_TestCaseNames method must return a variant array of the names of the available test methods.
- The ITestContainer_RunTestCase method must invoke the method name specified in the TestCase input parameter. This can be done using InvokeHook, CallByName or by building a case statement of the methods to execute. InvokeHook is the preferred method for invoking a named method. It does, however, require the project to have a reference to TLBINF32.DLL. CallByName also works and does not require the extra reference; however, it will mangle exeception information if an exception occurs in the invoked test method.
- The ITestContainer_Setup and ITestContainer_TearDown are used to set up and tear down the test fixture. These methods are invoked before and after (respectively) each test method.
- All test methods must be public methods that accept a TestResult as an input parameter. Test method names should be included in the variant array returned by the ITestContainer_TestCaseNames method. The TCTestContainer class contains a single test method by default called testSample. Delete this method once you begin to write your own test methods.
Creating a new Test Method
All you have to do to begin writing your own test methods is to add a public method that accepts a TestResult as an input parameter. To ensure that this test method can be run from the TestRunner, make sure that you add the method name to the the variant array returned by the ITestContainer_TestCaseNames method.In your test method, you should use the TestResult input parameter as a means for tracking the success or failure of your test. The TestResult object contains a variety of methods for determining the success or failure of a given test. The most commonly used is the Assert method. You use this method to determine the success or failure of a boolean condition. If the condition fails, this failure is recorded and displayed in the TestRunner control. The sMessage parameter is the message that you would like to log if the test fails. Note that successful tests are not logged.
The TestResult object also contains an AddFailure method if you have a particular failure that you want to log that hasn't been produced as the result of a boolean condition. There is also a AddError method that can be used to catch unexcepted exceptions. These will typically be caught in the RunTest method; however, if you choose to do custom exception handling in your test method, use this method to log unhandled exceptions.
Page updated: 2002-09-16