How To Meet Salesforce Test Code Coverage Requirement: A Complete Guide

How To Meet Salesforce Test Class Code Coverage Requirements For Deploying Or Packaging Apex And Related Questions that release manager faces during deployments will be discussed on this article. As a release manager of a project when you deploy a salesforce package to a production environment, you might notice your heart beating abnormally fast.Well if that is the case then I am pretty sure you don’t understand the concept of running test classes in production. In 2016, I was in the same boat as you when I launched my first production deployment.I was not understanding what are these different options of executing test classes and how can I force deploy my code if my local tests are failing in production. In this article I am going to share some useful tips regarding test class execution in a production environment. 

Salesforce Test Class Code Coverage Requirement

Before deep diving further let’s first understand some basic questions that are being asked by salesforce developers, admins and release managers with respect to code coverage.

Q1) What is the minimum code coverage required in salesforce?

Ans) 75% when you are deploying your code to production and for sandboxes there is no code coverage requirement.

Q2) Where to check overall code coverage in salesforce or what should developers do to check code coverage ?

Ans) There are several ways to check overall code coverage and code coverage with respect to an apex code. To check overall code coverage you you can go to salesforce setup | Quick Find Apex class | Click on “Estimate Your Organization’s Code Coverage” and you should see the overall code coverage of your organization.

 

 

There are also other ways to check code coverage by running all test classes in your org or from developer console, but we will cover those in the upcoming section. To summarize above points, if you are getting an error while deploying your code to salesforce it means that your overall code coverage to deploy in production is less than 75% for apex code. Before jumping to the next paragraph I would also like to mention one more important point regarding overall code coverage which is very important to understand when you are deploying code to production. 

Important Example To Understand Execution Of Apex Code Coverage

Consider an apex testing example, that you have 500 Apex Class in production and the organization code coverage is 79%. But, during the next deployment you are deploying 200 more apex classes, so now if you think that you can cover 75% org code coverage requirement then you are wrong. So, if you do the basic math 500 Apex Classes + 200 Apex Classes = 700 Apex Classes, hence, you have to cover overall code coverage for 700 Apex classes and not 500 Apex Classes.This is one of the use cases which we always forget when we deploy new apex code in production.

What are default options for running test classes during salesforce deployments?

Let’s review the different options that Salesforce provides for executing test classes in a Salesforce environment. We will also discuss its use cases so that you can select the right option when executing in production. As you know that you need to write test classes for apex class, apex trigger and flows. Moreover, Salesforce org can also have installed packages such as unlocked packages, managed packages and unmanaged packages. If you are building one of these packages, you need to have test classes associated with these packages.

I have also written an article about Salesforce Managed vs UnManaged vs Package.xml – A complete Guide

Default : All tests will be run with the default behaviour. All local tests are executed in production if any changes are made to Apex Classes, Apex Triggers, or Apex Flows that are part of the Salesforce package.In sandbox environment, no tests are executed. All tests, except those originating from managed packages, are local tests. In a package that does not contain Apex components, no tests are run.

Run Local Tests : When you select this option, all test classes in Salesforce are executed except for managed packages.

Run All Tests : When you select this option, all test classes in Salesforce are executed including all managed packages.

Run Specified Tests : Only the tests that the user specifies are executed. The user must create a list of all test classes in comma-separated form. The executed tests must cover the apex code coverage with a minimum of 75%. Each class or trigger is covered individually instead of the overall percentage.

How to run all test classes in Salesforce ?

To prevent deployment issues related to code coverage, it is very important that all the test classes are run in the organization. There are basically several ways to execute all test classes in salesforce. Let’s look at the first three most common ways of executing these classes.

How to run all test classes in Salesforce ?

 

Option 1: Using Apex Test Execution

  1. Go to Setup | Quick Find | Custom Code | Apex Test Execution.
  2. Click ‘Select Tests’
  3. Choose ‘Namespaces’ from the dropdown, and use the checkbox next to ‘Class Name’ to select all classes.
  4. Click ‘Run’.
  5. The status of the tests will appear in the list below.
  6. To view older tests, use the ‘View Test History’ link.

Option 2: Using Apex Class Run All Tests

  1. Go to Setup | Quick Find | Custom Code | Apex Classes
  2. Click ‘Run All Tests’.
  3. You will be redirected to the Apex Test Execution User Interface and the tests will be running.
  4. The status of each test is shown below.
  5. To view older tests, use the ‘View Test History’ link

Option 3: Using Developer Console

  1. Open the Developer Console by clicking on the Gear/Wrench icon.
  2. In order to start a new test execution, choose ‘New Run’ in the ‘Test’ menu.
  3. Select either ‘My Namespaces’ or ‘All Namespaces’.
  4. Click the checkbox next to the Class Name to select all classes.
  5. ‘Run’ the tests.
  6. Click on the “Tests” tab to see the currently running or previously run tests.
  7. View the ‘Overall Code Coverage’ to see the percentage of code coverage for each class and the entire salesforce organization.

Option 4: Other options using external Code Editors like VS Code

You can follow this salesforce document to explore this option: https://developer.salesforce.com/tools/vscode/en/apex/testing/

How To Abort Run All Test Classes In salesforce?

Imagine that you are working towards increasing code coverage at an organizational level and you have run all tests in Salesforce by choosing one of the above options. Since, your test classes in salesforce have been running since the last 5 hours and you have no idea when the tests will complete. You may find yourself in such a situation where your org is completely locked and you need some way to stop all those running Salesforce test classes. You can choose the below way to abort running test classes in salesforce.

  1. Go to Setup | Quick Find | Custom Code | Apex Test Execution
  2. Select All | Required Test Classes
  3. Click On Abort button on the columns tab for the currently running test classes

How Salesforce Calculates Code Coverage For Test Classes?

This is very important question to understand as there are situations when you have to do a hotfix deployment for an emergency release and you don’t have time to fix all the code coverage but your focus is to cover only 75%, so in that case you should know how salesforce calculates these classes mathematically.

 

There is a salesforce SOQL Query that you can use to calculate the code coverage. SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate WHERE ApexClassOrTrigger.Name = ‘Your Class/Trigger Name’

 

The above query will only work when you are using the Tooling API.

 

CODE COVERAGE % = No. Of Lines Of ApexClass/ApexTrigger

    —————————————————–

    No. Of Covered Line + No. Uncovered Lines

​​

#Note: The above code coverage will only be calculated on executable lines of code. It means that salesforce comments will not be used in calculating code coverage.

As far as running test classes are concerned, we’ve covered most of what every salesforce release manager should know. If you’re hiring one, these questions should be in the job description. But, we have still not discussed real time use case scenarios where release managers usually face issues and what are the best practices in terms of deploying apex code with your matching code coverage requirements.

How To Avoid Code Coverage Issues And Meet Salesforce Criteria Of 75%

There are certain best practices which I used to follow when matching code coverage in production.

  1. Developers Responsibilities: As a release manager, I assume that you know how to code and that you can create a document that outlines the do’s and don’ts of writing test classes. Some of them are mentioned below:

  • SeeAllData=false is set for all classes and useSeeAllData=true should not be used.

  • Ensure that no code is being slipped through try/catch blocks: if you’re running try/catch in the test case but ignoring the catch section, then it will decrease your code coverage.

  • Your test cases should use System.runAs and the appropriate user profile should be set up in your test cases.

  1. Release Managers Responsibilities:As a release manager, you should know the code coverage percentage in your lower environments before you push your code to production org.

  • Run all test classes during weekends in lower environments and make sure the overall code coverage should be more than 75%. You should strive for an overall coverage of 90% and greater.

  • In case of a test class failure, make sure to notify developers.

  • Follow up with developers to ensure they have fixed those failing classes.

  • Once they have fixed those failing classes, deploy them to the target environment and again run all test classes during weekends.

Best Practice for Matching Code Coverage During Production Deployment

I will discuss best practices based on typical problems faced by release managers during production deployments.

Q1) I am trying to push my code from the sandbox to production, I am encountering an error when I try to evaluate the deployment in the production environment:

Code Coverage Failure Your organization has a code coverage of 65%. In order to complete the deployment, you need 75% coverage.

Ans) As mentioned above, as part of the release management responsibilities, you are required to follow the above mentioned steps along with those during production deployments. But, before any goLive deployment, you have to validate the entire package against production deployment using Run Local Tests or Run Specified Tests and based on that you can figure out if the code coverage is more or less than 75% coverage.

Q2) We have more than 6000 apex classes, and it usually takes us around six hours to run them all in production. Is there a way to stop running all tests during validation/deployment in production?

Ans) This is the most common case when deploying a change to a production environment for large organizations. In such cases before any goLive deployment date, do a validation against the production deployment and then use Quick Deploy feature to deploy on the target org.

Secondly, Use Run Specified Tests and create a test suite for the apex code that you will be deploying during goLive date.

Q3) I’m trying to deploy a class to production and I get the following errors:

Code Coverage Failures: 1. Class: BusinessHoursAges — Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required, at least 75% test coverage is required. *********** DEPLOYMENT FAILED ***********

Ans) I have already answered this as part of Q1, Q2 and make sure you have more than 75%.

 

Let us also consider a scenario where it is very critical for you to deploy apex code in production but you are facing some issues related to salesforce code coverage. So in such cases, you can create a case with salesforce and give them a valid business reason for bypassing code coverage in production. Hence, in these scenarios, deploying Apex code to production does not require any code coverage. It is never recommended to do this as it will impact your future production releases.

In summary, as a release manager, it is your responsibility to understand how you execute your test classes in production and ensure the code coverage matches the logic defined by salesforce. 

We also write product reviews on the tools and if you have any product that you would like to get reviewed you can always comment, contact me via Contact Us Page and I will reply back you immediately 🙂

#Note: Publishing this content anywhere without the consent of SFDC247 will result in a lawsuit against copyright infringement

 

SFDC247

Leave a Reply

Your email address will not be published. Required fields are marked *