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:
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:
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.
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:
- Use @isTest at the Top for all the test classes.
- Always put assert statements for negative and positive tests.
- Use the @testSetup method to insert the test data into Test class that will flow all over the test class.
- 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.
- Use System.runAs() method to test the functionality in user Context.
- Do not put (seeAllData = true) in test class otherwise, use it for exceptional cases.
- Avoid Using Hard Coding Ids anywhere in test Class or any apex class.
- Please make sure that each class has minimum 75% coverage and also the main functionality has been covered. Target 100% code coverage.
- All class method must be tested for at least 20 records and keep the real scenarios in mind.
- 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:
- From Setup, enter Apex Test.
- Click Options...
- In the Apex Test Execution Options dialog, select Disable Parallel Apex Testing and then click OK.
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.