How to: Create a Test Runner Codeunit
You can create test runner codeunits to manage the execution of test codeunits and to integrate with test management or test reporting frameworks.
To create a test runner codeunit
In the development environment, on the Tools menu, choose Object Designer.
In Object Designer, choose Codeunit, and then choose New.
On the View menu, choose Properties.
In the Properties window, in the Subtype field, select TestRunner to specify that this is a test runner codeunit.
In the TestIsolation field, specify which changes to the that you want to roll back. You can choose from the following values:
Disabled
Codeunit
Function
For more information, see TestIsolation Property.
In the C/AL Editor, in the OnRun function, enter code to run the test codeunits. For example, the following code in the OnRun function of a test runner codeunit runs three test codeunits.
CODEUNIT.RUN(CODEUNIT::CustomerDiscount); CODEUNIT.RUN(CODEUNIT::TaxCalculationTest); CODEUNIT.RUN(CODEUNIT::ItemCostingTest);
Note
You may want to define your test suite in a table and then write code in the OnRun function of the test runner codeunit to iterate through items in the table and run each test codeunit.
(optional) To create an OnBeforeTestRun trigger, do the following steps:
On the View menu, choose C/AL Globals.
In the C/AL Globals window, on the Functions tab, on a new line in the Name field, enter OnBeforeTestRun, and then choose Locals.
In the C/AL Locals window, on the Parameters tab, enter the following.
Name DataType Length CodeunitID
Integer
CodeunitName
Text
30
FunctionName
Text
128
In the C/AL Locals window, on the Return Value tab, enter the following.
Name Return type OK
Boolean
Close the C/AL Locals window.
In the C/AL Editor, in the OnBeforeTestRun trigger, enter code that executes before each test function. Typically, the code in the OnBeforeTestRun function determines if the test function should execute and returns true if it should. Otherwise, the trigger returns false. The OnBeforeTestRun trigger may also initialize logging variables. For example, the following code initializes a logging variable and returns true to indicate that the test function should execute. This example requires that you create the following global variable.
Name DataType Before
DateTime
Before := CURRENTDATETIME; EXIT(true);
(optional) Do the following steps to create an OnAfterTestRun trigger:
On the View menu, choose C/AL Globals.
In the C/AL Globals window, on the Functions tab, on a new line in the Name field, enter OnAfterTestRun, and then choose Locals.
In the C/AL Locals window, on the Parameters tab, enter the following.
Name DataType Length CodeunitID
Integer
CodeunitName
Text
30
FunctionName
Text
128
Success
Boolean
Close the C/AL Locals window.
In the C/AL Editor, in the OnAfterTestRun trigger, enter code that executes after each test function. For example, the following code logs the results of the tests to the test reporting system. This example requires that you create a record variable named
log
.log.INIT; log.UnitId := CodeunitId; log.Unit := CodeunitName; log.Func := FunctionName; log.Before := Before; log.After := CURRENTDATETIME; If Success THEN log.Status := log.Status::Success ELSE BEGIN log.Status := log.Status::Failure; IF FunctionName <> '' THEN log.Message := GETLASTERRORTEXT; END log.INSERT(true);
Note
If you implement the OnAfterTestRun trigger, then it suppresses the automatic display of the results message after the test codeunit runs.
On the File menu, choose Save.
In the Save As window, in the ID field, enter an ID and in the Name field, enter a name for the codeunit. Verify that the Compiled check box is selected, and then choose OK.
See Also
Tasks
How to: Create Test Codeunits and Test Functions
How to: Create Handler Functions