Unit test framework
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.
To create a new test case to test functionality in an application, follow these steps:
Open Visual Studio as an administrator.
Select Open a project or solution.
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.
Right-click the Fleet Management solution in the Solution Explorer.
Select Add > New item.
Expand Dynamics 365 Items.
Select** Finance and operations** as the project type.
Name the new project FleetManagementUnitTestSample and specify the FleetManagement folder located on your desktop as the location.
Select Create.
Right-click on the new project and select Properties.
Set FleetManagementUnitTest as the Model property.
Select OK.
Right-click FleetManagementUnitTests project and select Add > New Item.
Note
This adds a test class containing the tests against the fleet management code.
Select Test Class as the type of element to add in the Add New Item page.
Right-click on the Test class and select Rename.
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"))); } }
Save the class. Two test cases will be available in the Test Explorer.
Right-click the FleetManagementUnitTestSample project in Solution Explorer > Build.
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:
- In Visual Studio, open Dynamics 365 > Model Management > Create model.
- Enter the model name that includes the word "test", Model publisher, Model description.
- Select Next, and then select Next again.
- 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
- Select Next.
- Select Finish.