Activity Forums Salesforce® Discussions What is @testSetup annotation? When you will use it?

  • Avnish Yadav

    Member
    August 3, 2018 at 6:21 am

    Hello Prachi,

    For example, we have a test class, It has two test methods and each test method required 5 account records. So without creating 5 records for each test method, we will create a new method with annotation @testSetup then create only 5 account records. These records can access all test methods using SOQL query in that test class.
    Ex: @testSetup method
    @testSetup static void setup() {
    // Create common test accounts
    List<Account> accountsLst = new List<Account>();
    for(Integer i=0;i<5;i++) {
    testAccts.add(new Account(Name = 'TestAc '+i));
    }
    Insert accountsLst;
    }
    Thinks to remember when you use @testSetup method:

    1. We can use only one test setup method per class.
    2. Test setup method will execute first in the test class.
    3. If any DML error occurs in this test setup method, the rest of the method not executed.
    4. If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

    Thanks.

  • Parul

    Member
    September 16, 2018 at 3:06 pm

    If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class.

    Records that are created in a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution.

    If a test method changes those records, such as record field updates or record deletions, those changes are rolled back after each test method finishes execution. The next executing test method gets access to the original unmodified state of those records.

    Syntax;

    @testSetup static void methodName() {

    }

    Ex:

    @iSTest
    public class LeadProcessorTest {

    //below test setup method will be invoked
    // implicitly before every test methods
    @testsetup
    static void createLead(){
    List<Lead> lstLead = new List<Lead>();
    for(Integer i = 0 ; i<200 ; i++) {
    lstLead.add(new Lead(lastName = 'testLName'+i , Company = 'Salesforce'));
    }
    insert lstLead ;
    }

    //Check whether records created in test method
    //is accessible or not
    public static testMethod void test(){
    System.assertEquals(200, [SELECT COUNT() FROM Lead Where company = 'Salesforce']);
    }
    }

     

    Thanks

  • Anjali

    Member
    September 21, 2018 at 1:37 pm

    Hi Prachi,

    All methods that are annotated with @testSetup will be called test setup methods.

    Few key points about TestSetUp methods:

    • Method marked with @TestSetUp annotation executes before any testMethod.
      Data created in this method doesn’t need to be created again and again, and it is by default available for all test methods.
    • There can be only one setup method per test class.
    • Test setup methods are supported only with the default data isolation mode for a test class. If the test class or a test method has access to organization data by using the @isTest(SeeAllData=true)annotation, test setup methods aren’t supported in this class.
    • Test setup methods are available for 24.0 or later versions only.
    • Every test method will get unchanged version of the test data created in setup method, doesn’t matter if any other test method has modified the data.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos