Monday, April 25, 2022

Automated testing for Customizations in Business Central

Hi All, 

Long blogs are something I do not write that often; but stay with me, it is going to be worth it! ;)

Microsoft provides a Test Toolkit with Business Central that allows you to manage and run automated tests. You can find the toolkit on the DVD in the applications\TestFramework folder.

Note: Added as an extension by Microsoft for new versions. To be able to use this toolkit, add that as a dependency in app.json

Once the Test Tool is installed, you can run the standard tests (if they were installed). Search for AL Test Tool using Search/Tell Me.

Tests in AL code are written as methods in codeunits.

Define the Codeunit as test: Set the SubType property in a codeunit to Test.

A Test codeunit can contain three types of methods:

Test method: Your main business login/use case that you want to test will be in this method. Declare the [Test] attribute on the method.

Handler method: These methods are used to automate tests by handling user interaction. It can test the content of a message box, handle a confirm dialog, and so on. Declare the [Handler] attribute on the method.

Normal method: These methods are normal or helping method which you can call in your Test method and helps to structure your code like any other method. 

Now, let us consider an example to put our knowledge into practice:

Use Case: If it is a VIP Customer, 'Priority' in Sales Order will be set to High. 

Prerequisites: 

  • A Custom field VIP in Customer
  • A Custom field Priority in Sales Order
  • The actual logic to set the Priority to High should be written in your main Codeunit. (What the test codeunit will do is simply automating the manual testing) 

How will you do manual testing?

  • You will create a Customer
  • Mark the field VIP as true
  • Create a Sales Order using that Customer

Result: The Priority will be set to High. 

We will write a code to automate the steps to check if our code works fine:



Note: Here we have used helping Libraries (Library - Sales, Library Assets) provided by Microsoft when you install the Test extension, which helps you to quickly write your test cases. 

Once you have deployed your extension, you will see your Test Codeunit and function. Follow these generic steps to run tests.

On the AL Test Tool page, click the Get Test Codeunits action and select the Tests you want to run You can also use the Get Test Codeunits by Range action to provide a range of codeunits object IDs. Codeunits you created yourself and published using extensions will also appear in this list.




After selecting one or both options, you'll get a list with the selected codeunits and all Test Functions within that codeunit. To run the tests, click the Run Tests action. You can select to run all functions, or only from the selected codeunit.


After running the tests, you'll get an overview of all tests that succeeded, but you'll also get a list of all failed tests. The Error Message column displays errors for any failed tests.


Note: The standard Test Runner Codeunit will be used, which will run the Tests and log the results of the test functions.

Reach out to me if you have any questions or suggestions. 

Check out other blogs, if you haven't already. 

Kindly suggest what else you want me to blog about. Keep following and supporting!

You can also email me at jyotsnaoberainav@gmail.com

Also, visit my website JONAV if you are looking for NAV/D365BC Training and/or Services!

Thank you! 

No comments:

Post a Comment