Principles Of A Good Test Class


Testing is a critical part of the development process and a key to successful long-term development in the Software Development Life Cycle (SDLC).

It helps us to find more bugs and exceptions in our code in the earlier stages of the development process. In this way, our code will be more error-free.

Use of Test Class in Salesforce

Now, we test our apex code (classes or triggers) by writing a test class for our apex code through which we can test our code against various cases like the positive case, negative case, corner use case, for a single record, or records in bulk to be ensured that our code is working as expected.

In Salesforce, our test class should have a code coverage of at least 75% to move our code into production. Otherwise, deployment will fail. It is the minimum criteria given by Salesforce but we should always try to keep the code coverage way more than 75%.

By proper testing, we make ensure that our code won’t break in a production environment.



public class TestClass{

static void testMethod(){

//code ..




Apex Test Class Best Practices

1. Test class should cover as many lines of code as possible which enables our code to move in the production smoothly.

2. If our code contains a conditional branch or logic, eg. if-else, our test class should cover each branch in the code.

3. Make calls using both positive and negative inputs to methods in our code.

4. We should use System.assertEquals() method so that our code behaves as expected.

5. We should not use (seeAllData = true) as it opens up the access of records in our org.

6. We should write our logic in Test.startTest() which gives the fresh governor limits.
We can write as many DML statements as we want in our Test method because when we write Test.startTest(), we will get a brand new set of governor limits.

7. Use Test.stopTest() which makes the ending of our test code.

8. Create test records from scratch using Test.setup() method.


  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.