Tuesday 15 November 2016

test class best practices in salesforce

Test class best practices in sales-force:


we need to use System.assert / System.assertEquals in the test. How would you know you are             getting  what is expected from the test? There have to be assert statements.
we need to avoid @isTest(seeAlldata = true) ?
        we have to setup our own data for testing purpose and should not rely on the Org data.
Provide a description of what you are test is doing(comments).
we need have setup method for all your data creation.
we should test for both positive and negative scenarios.
Tests are run as System Administrator only, is this how we do in the real system also? If not test        users needs to be created and by using  System.runAs () it has to tested as appropriate.
•     we should have only what exactly you are testing in between  Test.StartTest() and          Test.Stoptest().


Following are applicable for all the test classes.
The Ideal test class should be somewhat like this

@isTest
public class nameOfTestClass {

// any statics you want to define

// Have your setup methods defined here


/**
* Comments for the test method1
*
**/



static testMethod void testNameOfTheMethod1(){

// All the set up should be here

test.startTest();

// only what you want to test should be here.

test.stopTest();

}
/**
* Comments for the test method2
*
**/

static testMethod void testNameOfTheMethod2(){

// All the set up should be here

test.startTest();

// only what you want to test should be here.

test.stopTest();
}
}

Test Class Best practises










No comments:

Post a Comment

Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...