Before we talk on best practices we should be following while writing a test class, let’s discuss what a test class is and why it is needed.
Test class in Salesforce is a kind of framework which provides you a platform to supply data to your apex class and see how your apex is behaving. 
It is needed as testing is an important part of SDLC. So, before deploying your code to production environment, Salesforce requires at least 75% of your code to be covered by your test classes to make sure that your code doesn’t break in any situation in Production.

We should be following below points while writing test class:


  1. Use @isTest at the Top for all the test classes.
  2. Always put assert statements for negative and positive tests.
  3. Use the @testSetup method to insert the test data into Test class that will flow all over the test class.
  4. Always use Test.startTest() and Test.stopTest() doing this it will increase the governor limit of Salesforce. We also use this to increase the governor limit.
  5. Use System.runAs() method to test the functionality in user Context.
  6. Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.
  7. Avoid Using Hard Coding Ids anywhere in test Class or any apex class.
  8. Please make sure that each class has minimum 75% coverage and also the main functionality has been covered. Target 100% code coverage.
  9. All class method must be tested for at least 20 records and keep the real scenarios in mind.
  10. Use the ORDER BY keywords to ensure that the records are returned in the expected order. 


Best Practices for Parallel Test Execution

Tests that are started from the Salesforce user interface (including the Developer Console) run in parallel. Parallel test execution can speed up test run time. Sometimes, parallel test execution results in data contention issues and UNABLE_TO_LOCK_ROW error occur.
When a deadlock occurs in tests that are running in parallel and that try to create records with duplicate index field values. A deadlock occurs when two running tests are waiting for each other to roll back data. Such a wait can happen if two tests insert records with the same unique index field values in different orders.

This can be prevented by turning off parallel test execution in the Salesforce user interface:

  1. From Setup, enter Apex Test.
  2. Click Options…
  3. In the Apex Test Execution Options dialog, select Disable Parallel Apex Testing and then click OK.
Apex test execution option

Test classes annotated with IsTest(isParallel=true) indicate that the test class can run concurrently with more than the default number of concurrent test classes. This annotation overrides default settings.

5 thoughts on “Salesforce – Test Class Best Practice”
  1. An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!

  2. Spot on with this write-up, I truly think this website needs much more consideration. I?ll probably be again to read much more, thanks for that info.

Leave a Reply

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