Please find the best practices while writing a test class for actual classes/controllers/triggers code coverage.
1. Make sure you have used @isTest annotation in test class.
2. Test class should private and let's not create any dependency on the individual test class
@isTest
private class TestClassName{
// your test methods
}
3.Use @TestSetup method to create all records for your business logic.
4.Create a TestFactory class and place your reusable code and make sure this is public and have @isTest.
5.Create your sobject records and setup object records separately using System.RunAs(user) else you will get error called Mix DML is not allowed.
6.Create separate test methods for each functionalities.
7.Use SOQL query to get the records and perform your testing.
8.Use Sytem.RunAs(user) to run specific flow for specific user context.
9.Initialitise your common variable on class level and reuse.
10.After each DML operation, use soql query and then check the value using system.assertequals().
11.Use System.StartTest() and System.StoptTest() to execute asynchronous call like @future, Queable interface. This is also used to fresh start the salesforce governor limits.
12.Perform positive and negative test both.
System.assertEquals('Demo','Demo','Both the names are different');//Positive test
System.assertNotEquals('Demo','Demo','Both the names are same');//Negative test
13.Do not create duplicate literal value instead create a public static final variable
14.Test bulkification.
15.Use @TestVisible on private members to expose them into test class.
16.Use assert after actual method calls to validate business logic.
Happy Learning!!
Comments
Post a Comment