Blog

Code Coverage For PROD In Salesforce

  • (4.0)
  • | 1135 Ratings
Code Coverage For PROD In Salesforce

Apex unit testing makes sure that your Apex code is with high quality and meets the requirements in deploying Apex.

In the testing framework, your code is tested and the testing code is coded in the sandbox environment, and then deployed to production Org. Apex unit tests are also required for distributing and deploying the Apex in Salesforce Org. The Apex code ensures that it works as expected. We write the unit testing code in order to find the bugs in our code and for fixing the output. Testing enables the code coverage, and in order to this, first, we need to know what is Code Coverage. Let’s learn what code coverage is now. Code Coverage is nothing but the percentage of code working. The minimum code to be covered is 75% in order to deploy in production from Sandbox.

Percentage of code coverage: For the code to be moved to the production, the code coverage for APEX Class - 75%,
Trigger Class - 1%

Code Coverage: How many lines we are covering by our Test Class, i.e., called Code Coverage in Salesforce?

Apex Class:

Apex Class

TestClass:

Test Class

Trigger Class:

Trigger Class

How to find the amount of code covered in APEX Class?

Open the Test Class in the Developer Console → Anonymous Window → File → Open → Test Class → Click on “Run Test” button → Open bottom of the window → Open the “Tests” Tab.

Testclasspass

Testclasspass

How to Highlight the Code that is executed?

Now, open the APEX class → “Code Coverage” and press the downward arrow button → Select “All Tests 100%

Codecoverage100percent


Codecoverage100percent

You can find some of the code highlighted with blue color on the screen. It shows the code executed for the “Test Class”.

Codehighlighted

Code highlighted

Now, let us look into deeper concepts, we will change the business logic in the APEX Class and look into the changes occurs.

Let us include different business logics with different discount in the APEX Class, and look for the code coverage changes.

Now, run the test from the “Test Class”, and look for the changes that occur. And now, open the APEX Class program and Highlight the code in the previous process (How to Highlight the Code that is executed?).

In the above image, partial code is highlighted with blue color as it is executed, and the remaining in red in color as it is not executed because of the value inserted into the DataBase via the program. The amount of code executed is 40%, so the code cannot be moved to Salesforce Production Environment. This is because it should reach a minimum of 75% to get into the production environment.

Now, we will try to include different functions in the “Test Class” for the purpose of data insertion with different values and look for the Code Coverage percentage and the code execution from “Test Class” → Run Test.

Codehighlightedafter60

Codehighlightedafter60

In the above code, 60% of the code is executed, and still, it is not possible to move to PROD (Salesforce Org). Now, we will look into the total code coverage with all the possible functions in the “Test Class” -- Run Test.

Codehighlightedafter100

Codehighlightedafter100 

In the above code, 100% of the code is executed, and now it is possible to move to PROD(Salesforce Org).

How to find the amount of code covered in APEX Trigger?

As discussed in the previous article, not only APEX Code but also the Trigger Code must be above 1% or else the code will not be moved to the PROD(Salesforce Org). So, we will have a look at the code highlighted and code coverage percentage in “Trigger Class”.

Codehighlightedintrigger

Codehighlightedintrigger

The above figure indicates the code coverage of “Trigger Class”.

How actual calculation is done in PROD(Salesforce Org)?

It can be explained with the below example values. Let us assume that we are sending the below files to the PROD with the code coverage values like,

Example#1

TestClass1
ApexClass1 -- 75%
TestClass2
ApexClass2 -- 50%
TestClass3
ApexClass3 -- 80%
The average of all the three classes will be taken,
75 + 50 + 80 = 205/3 = 68%

Result: 

ERROR! These class won't be sent to PROD(Salesforce Org).

Example#2

TestClass1
ApexClass1 -- 90%
TestClass2
ApexClass2 -- 50%
TestClass3
ApexClass3 -- 90%
The average of all the three classes will be taken,
90 + 50 + 90 = 230/3 = 76%

Result: 

Salesforce will send all the code to PROD(Salesforce Org).

Note: The average of classes in the package will be taken into consideration by Salesforce to move the content or code into the PROD(Salesforce Org).

In the next topic, we will discuss in detail about “SOQL Of Salesforce”. Keep following us for more info on Salesforce Development/Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.

Arogyalokesh
About The Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.


DMCA.com Protection Status

Close
Close