oTest.$runtestcase()

Function group Execute on client Platform(s)
oTest NO All

Syntax

oTest.$runtestcase(cClass[, cMethods, pCallbackInst, cMethodName, pListener, iTimeoutMs, iTotalTimeoutMs])

Description

Runs one test class, or its named tests, then calls cMethodName on pCallbackInst with (rSummary, lResults). Returns an error message, or empty once the run has started.

$runtestcase() runs the tests of one Test Case class, or just the tests you name, and calls you back when the run is over. It returns an error message if the run cannot start, or empty once it has started.

Each test runs on a new instance of the class, so $construct runs before every test. The runner waits for an asynchronous test (one that sets $autosendresponse to kFalse) until it reports its result with $sendresponse or times out.

The parameters are as follows:

Parameter Description
cClass The test class, written library.class. It must be an object class whose superclass is .oTest.oTest.
cMethods Optional. The names of the tests to run, comma-separated, in the order to run them. A name may be given with or without its leading $. Empty, the default, runs every method of the class whose name starts with $test, in name order. Every name is checked before any test runs, so a misspelled name is reported as an error and nothing runs.
pCallbackInst Optional. The instance to call when the run is over, usually $cinst. It can be an object, window or other instance.
cMethodName Optional. The name of the method to call on pCallbackInst, as text. It is called with a summary row and a results list, described below.
pListener Optional. An instance that is told about the run as it goes. It needs only the methods it wants, out of those listed below.
iTimeoutMs Optional. How long each asynchronous test may take to report, in milliseconds. 0, the default, means 30 seconds. A test that takes longer is stopped and given the outcome timeout.
iTotalTimeoutMs Optional. A limit for the whole run, in milliseconds. 0, the default, means 120 seconds. When the limit is reached, the tests not yet started are reported with the outcome notRun.

For example, to run two tests of one class:

Do oTest.$runtestcase("myLib.oTest_Invoice","$testTotal,$testDiscount",$cinst,"$onTestsDone") Returns lErr

When the run is over

When the run is over, the method named cMethodName is called on pCallbackInst with two parameters: a summary row and a results list. If every test is synchronous, this happens before $runtestcase() returns; otherwise it happens later, when the last test finishes.

The summary row has these columns:

Column Description
ran The number of tests that ran.
passed The number of tests that passed.
failed The number of tests that did not pass: their outcome was failed, error, timeout or stopped.
notrun The number of tests that were never started, because the run was stopped or ran out of time.
classes The number of test classes in the run.
classesfailed The number of test classes that did not pass.
elapsedms How long the run took, in milliseconds.
outcome "passed" if every test in every class passed, otherwise "failed".

The results list has one line per test, with these columns:

Column Description
class The test class, written library.class.
method The name of the test method.
mode "sync" for a test that reported its result when its method returned, "async" for one that reported later by calling $sendresponse.
outcome passed, failed (an assertion failed, or the test called $adderror), error (a fatal runtime error, or the method could not be run), timeout, stopped or notRun.
elapsedms How long the test took, in milliseconds.
result The test's result, as JSON text.
errors The errors the test recorded, as a one-column list.

If the test class could not be run at all, for example because its library was closed after the run started, the results list has one line with an empty method and the outcome error.

If you omit pCallbackInst and cMethodName, the tests still run, but nothing is called when they finish.

Following the run

pListener is told about the run by calling these methods, if it has them:

Method Called
$onRunStarted(lTestNames) When the run starts. lTestNames lists the tests it will run, in order.
$onTestStarted(cMethod) When a test starts.
$onTestFinished(rResult) When a test finishes. rResult has the method, mode, outcome, elapsedms, result and errors of the test.
$onRunFinished(lResults) When the run is over, before the callback, with one line per test.
$onRunLog(cText, iLevel) When a test, or the runner, logs a line. iLevel is a kTestLogLevel... constant.

Test Case constants

Constant Description
kTestLogLevelInfo An information message.
kTestLogLevelWarning A warning.
kTestLogLevelError An error.

Errors

$runtestcase() returns an error message, and runs nothing, when:

One run at a time

Only one test run can be in progress at a time, whether it was started from the Test editor, the Test Suites window, code or the MCP server's execute_test tool. A second run is refused with the error "A test run is already in progress", followed by details of the run that is already in progress. The one exception is a run started directly by a test method while it executes: that run is part of the test, and goes ahead. A run started from a worker callback is refused like any other. Use oTest.$isrunning() to find out whether a run is in progress.