In this blogpost, we will learn about Test Coverage in Salesforce. Arguably the most important skill for Salesforce developers is to know how to migrate their customizations to a production environment.Although you can make changes to code directly in a Developer instance, in order to do so for a Production instance, you must first make your changes in a Sandbox and transition those changes to your Production instance.
Test Coverage in Salesforce
In order to deploy custom code to a production environment, Salesforce requires a minimum of 75% of your application code to be tested. What does this mean? Presumably (hopefully), you have already tested that your customization functions as intended before attempting to deploy. However, unless you wish to test the entirety of your application every time you make a small change, there needs to be some automated testing. This is where test coverage comes into play.
In essence, test coverage is specific code written in order to execute your application code for the express purpose of verifying that your customization behaves the way it is intended to. Code designated as test coverage does not count against your organization’s total lines of code limit, nor does it count against lines of code which must be covered by your test coverage. When we say that Salesforce requires 75% coverage, this means that at least 75% of your total lines of code must be executed by your test coverage.
The fact that test coverage is calculated based on total lines covered has bred a problem among newer and seasoned developers alike. Developers are more and more frequently starting to write test coverage which simply gets them line coverage rather than testing functionality. Part of the reason is economical (clients do not want to pay for the time to write proper test coverage, which can sometimes take longer than the actual customization desired), while other factors include certain untestable features (certain things cannot be tested properly due to platform limitations, although this list is diminishing), lack of knowledge by the developer, and simple negligence or laziness on the part of the developer.
Salesforce always recommends 100% test coverage. However, the required level of test coverage is 75% to deploy to a production server. Remember this if you plan on taking the developer certification exams.
“Bad” Test Coverage in Salesforce:
What I refer to as “bad” test coverage is test coverage that is designed to simply run lines of code to get to the necessary 75% coverage for deployment, without actually verifying any functionality.
“Good” Test Coverage in Salesforce
“Good” test coverage is what a developer will need to know before attempting the level 2 developer certification exam from Salesforce. A significant portion of the score is centered around writing test coverage (in fact, it takes longer to write adequate test coverage for the solution than to implement the solution itself).
According to Salesforce, “good” test coverage will cover multiple use cases, including specific restrictions and error handling, as well as assert frequently that the application logic is performing as intended.
Tests should also be run as mock users rather than only as a system administrator in order to verify access controls such as profile limitations and field level security.