Sunday, April 26, 2015

Salesforce Summer 15 New Feature || New way to calculate code coverage for multiline statements

In Summer 15 release, Salesforce changed the way to calculate code coverage for multiline statements. As a developer, you feel good in some situation and bad in some situations. In this post, I will explain both the situations.

Let me first explain the change -
If a single statement is broken into multiple lines, then each line will be considered now while calculating the code coverage. To be more precise, each line that contains an expression will be considered while calculating code coverage. Before Summer 15 release, multiline was considered as a single line.

To explain more, I will start with a simple code snippet.

Situation 1 - Happy Situation:
Consider the below code snippet.

Now say for example, you have written unit test methods for the methods calculateDistanceBetweenAB(), calculateDistanceBetweenBC(), calculateDistanceBetweenCD() and calculateDistanceBetweenDA().

You have also written test methods to cover line# 8,9 and 10. The only line which is not covered/tested is line# 11.

Now before Summer 15 release, the way in which code overage was calculated -
Total number of lines = 4 (line # 3,8,9 and 11)
Total number of lines covered = 3 (line # 3,8 and 9)
So the code coverage = 3/4 i.e. 75%

But after Summer 15 release, the way in which code overage will be calculated -
Total number of lines = 7 (line # 3,4,5,6,8,9 and 11)
Total number of lines covered = 6 (line # 3,4,5,6,8 and 9)
So the code coverage = 6/7 i.e. 85%

As you can see that the code coverage increases here. So for me, this is a happy situation. 

Situation 2 - Not So Happy Situation:
Consider the same code snippet.

Now say for example, you have written unit test methods for the methods calculateDistanceBetweenAB() only.

You have also written test methods to cover line# 8,9 and 10. 

Now before Summer 15 release, the way in which code overage was calculated -
Total number of lines = 4 (line # 3,8,9 and 11)
Total number of lines covered = 3 (line # 3,8 and 9)
So the code coverage = 3/4 i.e. 75%

But after Summer 15 release, the way in which code overage will be calculated -
Total number of lines = 7 (line # 3,4,5,6,8,9 and 11)
Total number of lines covered = 3 (line # 3,8 and 9)
So the code coverage = 3/7 i.e. 42%

So this is not so happy situation as you can see that code coverage decreases here. But there is always a way. You can write more test methods to increase the code coverage.

Please check the below details for your reference -

2 comments: