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

by Sudipta Deb | Aug 20, 2020 | Advanced Apex, Apex, cli, code coverage, SFDX, Spring 20, test class, VS Code | 0 comments

 

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 this option, developer will get – 

  • Different classes being covered by the test class. The percentage of each class covered and uncovered lines from each class.
  • Test result which will pass/fail result for each test method.
  • Finally Test Summary will show “Test Run Coverage” and “Org Wide Coverage”.
This change was introduced in Spring 20 release. You can read more about it – https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_sf_cli_code_coverage_apextests.htm
In order to use this feature in Salesforce CLI, the first thing you need to do is set up your environment variable.

Setting up the environment variable

There are three ways to do that. 
You can do that for a single command by doing 

SFDX_IMPROVED_CODE_COVERAGE=true sfdx force:apex:test:run ...
or set it for a shell session with

export SFDX_IMPROVED_CODE_COVERAGE=true

or if you want to set it permanently, then set it in the config file for your shell/command prompt.
Setting up the environment variable is important as it will eliminate Developer Console and VS Code/CLI code coverage mismatch.

Running the test

Once the environment variable is set up, you can run your test by –

sfdx force:apex:test:run -n “CommunicationHandlerTest” -c -r human

Here I am trying to execute the test class – CommunicationHandlerTest. To know more about the options, read – https://github.com/forcedotcom/salesforcedx-vscode/issues/2224
The result will be something like this 
I highly recommend everyone to use this new feature. Thank you.

Written by

Related Posts

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

For years, Salesforce developers have faced a common challenge: programmatically retrieving picklist values that are specific to a certain Record Type directly within Apex. The “solutions” often involved either complex SOQL queries against metadata, relying on the UI API (with its associated callout limits and serialization overhead), or maintaining clunky custom metadata/settings.

Good news, Technical Potpourri readers! The Salesforce Spring ’26 release is bringing a game-changer that will significantly simplify your Apex code and improve performance: native Apex methods to filter picklist values by Record Type!

read more...
Apex Cursor and Its Advantages Over Batch Processing

Apex Cursor and Its Advantages Over Batch Processing

Struggling with large datasets and SOQL governor limits in Salesforce? In this video, I will dive deep into Apex Cursors, the powerful new feature that revolutionizes how we process millions of records.

Apex Cursors allow developers to navigate through query results in manageable chunks within a single transaction or across Queueable jobs. In this video, I will show you the correct way to implement Apex Cursor and also when this is an exception, how to handle that and perform retry.

read more...

0 Comments

Leave a Reply