NoteNote

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

  1. In the Classic client, on the Tools menu, click Object Designer.

  2. On the Tools menu, click Options.

  3. In the Options window, in the Show C/AL Testability properties field, enter Yes, and then click OK.

  4. In Object Designer, click Codeunit, and then click New.

  5. On the View menu, click Properties.

  6. In the Properties window, in the Subtype field, select TestRunner to specify that this is a test runner codeunit.

  7. 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.

      CopyCode imageCopy Code
    CODEUNIT.RUN(CODEUNIT::CustomerDiscount); 
    CODEUNIT.RUN(CODEUNIT::TaxCalculationTest); 
    CODEUNIT.RUN(CODEUNIT::ItemCostingTest); 
    
    NoteNote

    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.

  8. (optional) To create an OnBeforeTestRun trigger, do the following steps:

    1. On the View menu, click C/AL Globals.

    2. In the C/AL Globals window, on the Functions tab, on a new line in the Name field, enter OnBeforeTestRun, and then click Locals.

    3. In the C/AL Locals window, on the Parameters tab, enter the following.

      Name DataType Length

      CodeunitID

      Integer

       

      CodeunitName

      Text

      30

      FunctionName

      Text

      30

    4. In the C/AL Locals window, on the Return Value tab, enter the following.

      Name Return type

      OK

      Boolean

    5. Close the C/AL Locals window.

    6. 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

        CopyCode imageCopy Code
      Before := CURRENTDATETIME;
      EXIT(true);
      
  9. (optional) Do the following steps to create an OnAfterTestRun trigger:

    1. On the View menu, click C/AL Globals.

    2. In the C/AL Globals window, on the Functions tab, on a new line in the Name field, enter OnAfterTestRun, and then click Locals.

    3. 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

       

    4. Close the C/AL Locals window.

    5. 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.

        CopyCode imageCopy 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);
      
      NoteNote

      If you implement the OnAfterTestRun trigger, then it suppresses the automatic display of the results message after the test codeunit runs.

  10. On the File menu, click Save.

  11. 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.

See Also