Category: code coverage

Spring’20 Brings Improved Code Coverage Result for Apex Test Class

 Writing test classes to cover both positive, negative, and bulkified scenarios are always the best practices that each developer should follow. While writing test classes, our approach should be to cover as much as code and all the scenarios. There is a restriction from Salesforce that if there is not enough coverage (75%) for the code, then Salesforce will not allow you to deploy your code to production.So identifying the code coverage is very important. Now there are multiple ways we can get to know the code coverage like – Setup menu, Developer Console, SOQL Query, Salesforce CLI, or Salesforce extension in Visual Studio Code. With so many options there comes the difficulties. Each option presents the code coverage in a different way and more importantly, each option calculates code coverage in a different way. This always creates confusion. For example, when a developer writes the test class and executes that from CLI, it provides the code coverage, let’s say 90%, which should be good enough to deploy the code into Production. But while deployment, the code coverage came down to 50%, and thus deployment stops. The reason for this drop in code coverage is that while deploying code coverage is calculated at the org level i.e. percentage of coverage for that class across the org.To solve this problem, Salesforce comes up with the Enhanced Code Coverage option. With...

Read More

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...

Read More
Loading