Listening for Unit Test Results

Before you run unit tests, you can create and register a unit test listener, which is an object that implements the JadeTestListenerIF interface. The interface methods for this object are invoked at certain stages during the test run by the setTestListener method of the JadeTestRunner class. Relevant test information is passed as parameters to the interface methods of the test listener.

The purpose of a test listener is to determine what is done with the unit test results. For example, you can display them on a form or capture them to a file. There can be at most one test listener. If there is no test listener, test results are written to the Jade Interpreter Output Viewer window.

To present the unit test results on a form, the form must implement the JadeTestListenerIF interface. For details about implementing the JadeTestListenerIF interface for a class selected in the Class Browser of a user schema, see "Implementing an Interface" and "Adding and Maintaining Interfaces", in Chapter 14 of the JADE Development Environment User’s Guide.

When the form loads, create an instance of the JadeTestRunner class to run the unit tests. Before the tests run, use the setTestListener method of the JadeTestRunner instance to register the form as the test listener. The following example shows how this is achieved in the load method.

load() updating;
vars
    tests : ObjectArray;
    jtr : JadeTestRunner;
begin
    create tests transient;
    tests.add(TestCalculator);
    create jtr transient;
    jtr.setTestListener(self);
    jtr.runTests(tests);
epilog
    delete tests;
    delete jtr;
end;

To capture the results of a unit test run to a file, add a class in your user schema with a reference of type File and make the class implement the JadeTestListenerIF interface. Create an instance of the JadeTestRunner class to run the unit tests. Before the tests begin, create an instance of your class and use the setTestListener method of the JadeTestListener instance, to register the instance as the test listener, as shown in the following example.

vars
    tests : ObjectArray;
// Listener class implements JadeTestListener and has reference of type File
    listener : Listener;
    jtr : JadeTestRunner;
begin
    create listener transient;
    create listener.file transient;
    listener.file.mode := File.Mode_Append;
    listener.file.fileName := "C:\UnitTests\results.txt";
    create tests transient;
    tests.add(TestCalculator);
    create jtr transient;
    jtr.setTestListener(listener);
    jtr.runTests(tests);
epilog
    delete tests;
    delete listener.file;
    delete listener;
    delete jtr;
end;

The following methods are reimplemented by a listener object.

The events during a unit test run that invoke callback methods are shown in the following table.

Method When the listener method is invoked …
finish After the last test method for the last JadeTestCase subclass completes
message Before the first test method for a JadeTestCase subclass starts or after the last test method completes
methodSuccess If a test method completes successfully without an exception being raised or an assertion failing
start Before the first test method for the first JadeTestCase subclass starts
testFailure If a test method results in an exception being raised or an assertion failing
testSkipped If a test method has the unitTestIgnore method option and is skipped
testSuccess For each individual assertion that passes, in each test method run