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: You should be able to run this project and see the UnitRunner form. So far it doesn't do too much. To make it do something, you need to start writing your own unit tests.

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:

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:

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