Improve unit test code coverage – Microsoft Dynamics AX

We know what are unit tests and we know how do we create unit tests in Dynamics AX and we also know why do we create Unit Tests. There are times when code coverage of Unit Tests is not 100% or is very less such as 50% or 60%. Ideally it should be at least 70% but one should try to get as much coverage as possible. There are times when code coverage cannot be 100% as there are such methods in a class or table that cannot have Unit Tests.

Code Coverage

Code Coverage is a measure used to describe the degree to which the source code of a method (or a unit) is tested by a particular test suite (unit test method).

Turn on Code Coverage recording in Dynamics AX

Below is a screen shot that shows how you can turn on the recording of Code Coverage

Turn on Code coverage option in Dynamics AX

Turn on Code coverage option in Dynamics AX

Steps for identifying code coverage issue

Note code coverage percentage after running tests

Note code coverage percentage after running tests

Note code coverage percentage after running tests

As seen from above screen shot, after running tests, go to detail and then click on Tests button. You will see the Code Coverage is 71.43.

Check if all the possible methods in a class or table have their UT methods written

Find which part of method is covered (green) and not covered (red)

In the above screen shot, the code with red color implies that it hasn’t been covered by your UT method code.

Improve Code Coverage

You have lots of methods and there is a chance, you haven’t written UT methods for all the methods. If this is the case, try to write UTs of all the methods present in a class or table. Ofcourse, not all methods can have UT methods, but try your best to write UT methods for all possible class/table methods.

There a few methods that have IF else conditions. If you have written a UT method and if you see a UT method that is testing only IF statement and not the else, try adding the code in UT method that will cover the else clause as well. This will improve code coverage by a few points. Don’t only check IF or else condition, check both.

Similarly if you have a SWITCH case, test all cases using the UT method and not only one case.

Once you follow these guidelines, you will see improvement in Code Coverage.

 

 

Abubaker Siddiq Shekhani

Abubaker Shekhani is an IT Entrepreneur and Full Stack Developer. He is the co-founder and the Developer behind Mytabeeb, a health care solution. He has worked for 5 years in Microsoft Dynamics AX space with Techno-functional role and glad to be one of few Microsoft Dynamics AX developers/consultants in Pakistan. He is TEDx speaker and likes to speak in public. He is an Amateur Astronomer and Astrophotographer. He is the founding member of Karachi Astronomers Society. He can be reached at me@abubakershekhani.com.