Unit test framework
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.
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.
- In finance and operations apps, go to Fleet management > Setup > Fleet setup.
- In the Data tab, select the Create button to generate the sample data set for Fleet management.
- A message displays when the data is created successfully.
First, you create a new project to run a unit test in. To create a new project, follow these steps.
- Open Visual Studio as an administrator.
- Select File > New > Project.
- In the Create a new project page, search for the Finance Operations project template.
- Select the Finance and Operations Project template, then select Next.
- In the Project name field, enter MyTestCase.
- Select Create.
- In the Solution explorer window, right click the MyTestCase project, and select Properties.
- In the Properties page, set the Model to Fleet management.
- Select Apply, then select OK.
Now, you must add a class to your project, to do this follow these steps.
Right click MyTestCase in the Solution explorer, then select Add > New item.
If the Add new item page shows in compact view like the following image, select Show all templates.
Expand the Dynamics 365 Items node in the left pane, and select Code.
In the Name field, enter MyTestClass1.
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.
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"))); } }
Save the class. Two test cases are available in the Test Explorer.
If the Test explorer is not visible, select View > Test explorer in Visual Studio.
Right-click the MyTestCase project in Solution Explorer and select Build.
In the Test explorer window, select the Test tube icon to view your tests, then select the Play icon to run your tests.
You can view the results of your test in the Text explorer window.