Unit test framework

Completed

You can use the SysTest Framework to write unit test code, integrate the tests, and then run the test to automate code testing. You can set up the SysTest Framework to create unit test from code. You can also import Task recorder recordings into Visual Studio to generate test code. More information about the Task recorder is provided later in this module.

Your test can then be integrated into a test module which can be used to manage test code. Adding the test module to the source control allows you to integrate your test with the build process. This addition runs your test code during the build to verify that all functionality is working as expected.

You can also run the test code individually. Running test codes is a repeatable process so, when the test class is created, you can run it multiple times. These iterations allow you to continuously validate your code changes. Additionally, quickly running tests to validate if your functionality still works after another developer has modified the element is key for productivity.

The option to rerun tests can also be beneficial to your organization's productivity during upgrades that are released by Microsoft. Instead of requiring user resources to regression test all the functionality from the user interface, you can run these unit tests that will enter the required data and run testing processes to determine if functionality is performing as expected.

Diagram of the Unit test model process from author tests, integrate tests and then run tests.

Exercise – Work with the test case framework

Note

To successfully complete this exercise, you must be using a finance and operations apps demo environment with the Contoso demo data and Fleet management module data.

To deploy the sample data set for Fleet management, follow these steps.

  1. In finance and operations apps, go to Fleet management > Setup > Fleet setup.
  2. In the Data tab, select the Create button to generate the sample data set for Fleet management.
  3. A message displays when the data is created successfully.

Screenshot of the Sample data created message.

First, you create a new project to run a unit test in. To create a new project, follow these steps.

  1. Open Visual Studio as an administrator.
  2. Select File > New > Project.
  3. In the Create a new project page, search for the Finance Operations project template. Screenshot of the Create a new project page.
  4. Select the Finance and Operations Project template, then select Next.
  5. In the Project name field, enter MyTestCase.
  6. Select Create.
  7. In the Solution explorer window, right click the MyTestCase project, and select Properties.
  8. In the Properties page, set the Model to Fleet management.
  9. Select Apply, then select OK.

Now, you must add a class to your project, to do this follow these steps.

  1. Right click MyTestCase in the Solution explorer, then select Add > New item.

  2. If the Add new item page shows in compact view like the following image, select Show all templates.

    Screenshot of the compact view.

  3. Expand the Dynamics 365 Items node in the left pane, and select Code.

  4. In the Name field, enter MyTestClass1.

  5. Select Add to add the class to your project.

Next, you write code that will run a test when ran in Visual Studio, to do this follow these steps.

  1. In the Code editor, enter the following code:

    //Test unit class must be public and extend the SysTestCase class
    class MyTestClass1 extends SysTestCase
    {
    public void setup()
    {
        // Reset the test data to be sure things are clean
        FMDataHelper::main(null);
    }
    
    [SysTestMethodAttribute]
    // Using the [SysTestMethodAttribute] because the name of the class does not begin with “Test”.
    public void testFMTotalsEngine()
    {
        FMRental rental;
        FMTotalsEngine fmTotals;
        FMRentalTotal fmRentalTotal;
        FMRentalCharge rentalCharge;
        FMRentalTotal expectedtotal;
        str rentalID = '000022';
    
        // Find a known rental
        rental = FMRental::find(rentalID);
    
        // Get the rental charges associated with the rental
        // Data is seeded randomly, so this will change for each run
        select sum(ExtendedAmount) from rentalCharge
         where rentalCharge.RentalId == rental.RentalId;
    
        fmTotals = FMTotalsEngine::construct();
        fmTotals.calculateRentalVehicleRate(rental);
    
        // Get the totals from the engine
        fmRentalTotal = fmTotals.totals(rental);
    
        // Set the expected amount
        expectedTotal = rental.VehicleRateTotal + rentalCharge.ExtendedAmount;
    
        this.assertEquals(expectedTotal,fmRentalTotal);
    
    }
    
    [SysTestMethodAttribute]
    public void testFMCarValidateField()
    {
        FMCarClass fmCar;
    
        fmCar.NumberOfDoors = -1;
        this.assertFalse(fmCar.validateField(Fieldnum("FMCarClass", "NumberOfDoors")));
    }
    
    }
    
  2. Save the class. Two test cases are available in the Test Explorer.

  3. If the Test explorer is not visible, select View > Test explorer in Visual Studio. Screenshot of the Test explorer.

  4. Right-click the MyTestCase project in Solution Explorer and select Build.

  5. In the Test explorer window, select the Test tube icon to view your tests, then select the Play icon to run your tests.
    Screenshot showing the test icons.

  6. You can view the results of your test in the Text explorer window. Screenshot showing the completed test.