Note |
---|
The content in this topic only applies to Microsoft Dynamics NAV 2009 SP1. For Microsoft Dynamics NAV 2009 content, see Developer and IT Pro Help for Microsoft Dynamics NAV 2009. |
In Microsoft Dynamics NAV 2009 SP1, 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 Classic client, on the Tools menu, click Object Designer.
-
On the Tools menu, click Options.
-
In the Options window, in the Show C/AL Testability properties field, enter Yes, and then click OK.
-
In Object Designer, click Codeunit, and then click New.
-
On the View menu, click Properties.
-
In the Properties window, in the Subtype field, select TestRunner to specify that this is a test runner codeunit.
-
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.
Copy Code 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, click 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 click Locals.
-
In the C/AL Locals window, on the Parameters tab, enter the following.
Name DataType Length CodeunitID
Integer
CodeunitName
Text
30
FunctionName
Text
30
-
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
Copy Code Before := CURRENTDATETIME; EXIT(true);
-
-
(optional) Do the following steps to create an OnAfterTestRun trigger:
-
On the View menu, click 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 click Locals.
-
In the C/AL Locals window, on the Parameters tab, enter the following.
Name DataType Length CodeunitID
Integer
CodeunitName
Text
30
FunctionName
Text
30
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
.Copy Code 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, click Save.
-
In the Save As window, in the ID field, enter 50006. In the Name field, enter TestRunner. Verify that the Compiled check box is selected, and then click OK.