Compartir a través de


Walkthrough: Testing Vendor Discount

Before you release a customized Microsoft Dynamics NAV 2013 application to a production environment, you must test the application. This walkthrough demonstrates how to use the test codeunits and test runner codeunits in Microsoft Dynamics NAV 2013 to test an application.

About This Walkthrough

This walkthrough illustrates the following tasks:

  • Creating a test codeunit and test function.

  • Adding a test to a test runner codeunit.

Prerequisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2013 with a developer license.

  • The CRONUS International Ltd. demo data company.

Story

Isaac is a business application developer working for CRONUS International Ltd. He has modified codeunit 70, Purch-Calc.Discount, which is a codeunit in the CRONUS International Ltd. database. Isaac wants to test the functionality of his customized codeunit before he offers the customized application for sale. He creates a new test codeunit with a new test function to test the Purch-Calc.Discount codeunit.

Isaac has already created several other test codeunits to test other modifications to his application, and he has a test runner codeunit to run these tests. He wants to add the new test for the Purch-Calc.Discount codeunit to the test runner codeunit so that he can run the test automatically with his suite of other tests.

Creating a Test Codeunit and Test Function

Isaac creates a new codeunit and specifies that it is a test codeunit. He creates the test function, which tests the Purch-Calc.Discount functionality, to this test codeunit.

To create the test codeunit and test function

  1. In the development environment, on the Tools menu, choose Object Designer.

  2. In Object Designer, choose Codeunit, and then choose New.

  3. On the View menu, choose Properties.

  4. In the Properties window, in the Subtype field, select Test to specify that this is a test codeunit.

  5. On the View menu, choose C/AL Globals.

  6. In the C/AL Globals window, on the Functions tab, enter CalculateVendorDiscount. This is the name of the test function.

    Note

    By default, functions that you add to test codeunits have the FunctionType property set to Test.

  7. On the Functions tab, choose Locals.

  8. In the C/AL Locals window, on the Variables tab, enter the following variables, which you will use in the CalculateVendorDiscount test function.

    Name DataType Subtype

    VendorDiscount

    Record

    Vendor Invoice Disc.

    PurchaseLine

    Record

    Purchase Line

    Discount

    Decimal

     

    PurchCalcDisc

    Codeunit

    Purch-Calc.Discount

    Important

    Be sure to create these variables on the Variables tab, not on the Parameters tab. If you create them on the Parameters tab, then you get an error when you compile that says the test method signature is invalid.

  9. In the C/AL Locals window, on the Text Constants tab, in the Name field, enter VendorDiscountError. In the ConstValue field, enter Vendor Discount Error - Line Amount: %1, Discount: %2, Discount Amount: %3.

  10. Close the C/AL Locals window and the C/AL Globals window.

  11. In the C/AL Editor, in the CalculateVendorDiscount function, enter the following code.

    Discount := RANDOM(99) + 1; // Set Discount > 0, <= 100
    // Find purchase line.
    PurchaseLine.SETFILTER("Line Amount", '>0');
    PurchaseLine.SETFILTER("Allow Invoice Disc.", '=%1', TRUE);
    IF NOT (PurchaseLine.FINDFIRST) THEN
      ERROR('No Purchase Line found for the Calculate Vendor Discount test');
    // Create vendor discount.
    WITH PurchaseLine DO BEGIN
      IF NOT (VendorDiscount.GET("Buy-from Vendor No.", "Currency Code", "Line Amount")) THEN BEGIN
        VendorDiscount.INIT;
        VendorDiscount.Code := "Buy-from Vendor No.";
        VendorDiscount.VALIDATE("Currency Code","Currency Code");
        VendorDiscount.VALIDATE("Minimum Amount","Line Amount");
        VendorDiscount.INSERT(TRUE);
      END;
    END;
    VendorDiscount.VALIDATE("Discount %", Discount);
    VendorDiscount.MODIFY(TRUE);
    // Run codeunit "Purch.-Calc.Discount" for calculating discount.
    PurchCalcDisc.RUN(PurchaseLine);
    PurchaseLine.GET(PurchaseLine."Document Type",PurchaseLine."Document No.",PurchaseLine."Line No.");
    // Validate purchase discount amount
    WITH PurchaseLine DO BEGIN
      IF NOT (ROUND("Line Amount" * Discount / 100) = "Inv. Discount Amount") THEN
        ERROR(VendorDiscountError, "Line Amount", Discount, "Inv. Discount Amount" );
    END;
    

    The code in this test function prepares the test data by setting a random discount amount, getting a record from the Purchase Line table that satisfies two filters, and creating a record in the Vendor Invoice Disc. table with the random discount amount. Next, it runs the Purch-Calc.Discount codeunit, which contains the code that is being tested. Finally, it validates the results of running the Purch-Calc.Discount codeunit and raises an error if the results are not as expected.

    Note

    This test code does not guarantee that the state of the database after you run the test is the same as the state of the database before you run the test.

  12. On the File menu, choose Save.

  13. In the Save As window, in the ID field, enter 50005. In the Name field, enter VendorDiscount. Verify that the Compiled check box is selected, and then choose the OK button.

Isaac can now create additional test functions in the VendorDiscount test codeunit to test other aspects of vendor discounts. These test functions should include negative tests, which validate that the code being tested works as intended under failing conditions.

Adding a Test to a Test Runner Codeunit

Isaac adds a line to his existing test runner codeunit that runs the new VendorDiscount codeunit. The test runner codeunit runs all the test codeunits that Isaac has created to test his customized functionality.

To add a test to a test runner codeunit

  1. In the development environment, on the Tools menu, choose Object Designer.

  2. In Object Designer, choose Codeunit, select the existing test runner codeunit, and then choose the Design button.

    Note

    If you do not have an existing test runner codeunit, you can create a new one. For more information, see How to: Create a Test Runner Codeunit. You do not need to create the OnBeforeTestRun and OnAfterTestRun triggers for this walkthrough. If you create the OnAfterTestRun trigger, then it suppresses the automatic display of the results message after the test codeunit runs.

  3. In the C/AL Editor, in the OnRun function, add the following code.

    CODEUNIT.RUN(CODEUNIT::VendorDiscount);
    
  4. On the File menu, choose Save.

Next Steps

The next steps are:

  • Create additional test codeunits to test different parts of the customized application.

  • Add the test codeunits to the test runner codeunit.

  • Run the test runner codeunit.

If you run the test runner codeunit, then you get a message window that displays the following text:

“Test Code Unit 50005 Vendor Discount
CalculateVendorDiscount: SUCCESS
SUCCESS”

If you added additional test codeunits or additional test functions, then the message window also displays the results of those tests.

See Also

Concepts

Testing the Application