Running Unit Tests from an Application

You can run tests from code, by creating an instance of the JadeTestRunner class and using the runTests method, which executes the test methods of all test classes in the collection specified in the tests parameter. The tests parameter is a collection of unit test classes and individual unit test methods. For a unit test class included in the collection, all unit test methods are executed in turn. You can specify classes and method together.

The following example shows tests being run for the TestCalculator class.

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

The following example shows tests being run for the TestConvertor class and for two unit test methods of the TestCalculator class.

vars
    tests : ObjectArray;
    jtr : JadeTestRunner;
begin
    create tests transient;
    tests.add(TestConvertor);
    tests.add(TestCalculator::add);
    tests.add(TestCalculator::divide);
    create jtr transient;
    jtr.runTests(tests);
epilog
    delete tests;
    delete jtr;
end;

The destination and format of the output from the runTests method depends on whether a test listener has been set up by using the setTestListener method of the JadeTestRunner class. A test listener is an object that implements the JadeTestListenerIF interface.

If a test listener is present, the implementation of the interface methods determines the destination and format of the output. If there is no test listener, test results are written to the Jade Interpreter Output Viewer window. The following output is produced from the TestCalculator unit tests without a test listener.

hello
FAIL: TestCalc::divide - assertEquals - expected 5 but actual= 4
FAIL: TestCalc::subtract - assertEquals - expected 8 but actual= 9
bye bye
Summary
FAILURES!!!
Total tests run: 6,  Ignored: 1,  Failures: 2

For more details about setting up a test listener, see "Listening for Unit Test Results", in the following section.