Unit test framework

Completed

Visual Studio includes a SysTest Framework that allows you 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 and FormAdaptors. Adding the test module to the source control will allow you to integrate your test with the build process. This addition will then run 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 execute tests.

To create a new test case to test functionality in an application, follow these steps:

  1. Open Visual Studio as an administrator.

  2. Select Open a project or solution.

  3. Select **FleetManagement solution **from your desktop folder.

    Note

    If the solution file is not on your computer, go to End-to-end scenario for the Fleet Management sample application for steps on how to create it.

  4. Right-click the Fleet Management solution in the Solution Explorer.

  5. Select Add > New item.

  6. Expand Dynamics 365 Items.

  7. Select** Finance and operations** as the project type.

  8. Name the new project FleetManagementUnitTestSample and specify the FleetManagement folder located on your desktop as the location.

  9. Select Create.

  10. Right-click on the new project and select Properties.

  11. Set FleetManagementUnitTest as the Model property.

  12. Select OK.

  13. Right-click FleetManagementUnitTests project and select Add > New Item.

    Note

    This adds a test class containing the tests against the fleet management code.

  14. Select Test Class as the type of element to add in the Add New Item page.

  15. Right-click on the Test class and select Rename.

  16. Name the new class FMUnitTestSample.

    class FMUnitTestSample extends SysTestCase
     {
       public void setup()
       {
    	 // Reset the test data to be sure things are clean
    	 FMDataHelper::main(null);
       }
    
      [SysTestMethodAttribute]
       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")));
      }
    
     }
    
  17. Save the class. Two test cases will be available in the Test Explorer.

  18. Right-click the FleetManagementUnitTestSample project in Solution Explorer > Build.

  19. Select Test > Run All Tests. The results will display when the Test Explorer is complete.

You can create a test module to help manage your test codes. Adding test modules to source control allows the build process to find modules that contain the word "test" in the name and implement the test run.

To create a test module, follow these steps:

  1. In Visual Studio, open Dynamics 365 > Model Management > Create model.
  2. Enter the model name that includes the word "test", Model publisher, Model description.
  3. Select Next, and then select Next again.
  4. Add references to the following models:
    • Application Platform
    • Application Foundation
    • Test Essentials
    • Application Foundation Form Adaptor
    • Application Platform Form Adaptor
    • Application Suite Electronic Reporting Integration
    • Application Suite Form Adaptor
  5. Select Next.
  6. Select Finish.