Tuesday 21 February 2017

What are the best practices while writing TestClasses in Salesforce?

What are the best practices while writing TestClasses in Salesforce?

  • Test coverage target should not be limited to 75%, we need to cover as much as possible. It is not about coverage it is all about testing complete functionality.
  • If possible don't use seeAllData= true.
  • Use Test.StartTest() to reset the governor limits in Test methods.
  • Use Test.StopTest() to make sure that operation has completed.
  • Use @TestVisible annotation to access private members and methods inside Test Class.
  • Use @testSetup annotation to improve the test execution performance. Any record created in this method will be available to all test methods of that class.
  • Use Test.isRunnigTest() in your code to allow test classes to enter inside code block.
  • Use System.runAs() method to enforce OWD and Profile related testing
  • End your test class with "_Test", so that in list view, Main Class and Test class will come together.
  • If you object schema is not changing frequently, you can create CSV file of records and load it in static resource. This file is act as test data for the test classes.

No comments:

Post a Comment