Monday 4 May 2015

@testSetup ( Set Up Test Data for an Entire Test Class )

Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in every test method in the test class. Test setup methods can be time-saving when you need to create reference or prerequisite data for all test methods, or a common set of records that all test methods operate on.

Test setup methods can reduce test execution times especially when you’re working with many records. Test setup methods enable you to create common test data easily and efficiently. By setting up records once for the class, you don’t need to re-create records for each test method. Also, because the rollback of records that are created during test setup happens at the end of the execution of the entire class, the number of records that are rolled back is reduced. As a result, system resources are used more efficiently compared to creating those records and having them rolled back for each test method

private class CommonTestSetup 
 static void setup() 
  Account acct = new Account();
       acct.Name = '';
       acct.Industry = 'Technology';
  insert acct;
  Contact cont = new Contact();
       cont.FirstName = 'Amit';
       cont.LastName = 'Chaudhary';
       cont.AccountId = acct.Id;
  insert cont;
 static void testMethod1() 
  Account acct = [SELECT Id FROM Account WHERE Name='' LIMIT 1];
     acct.Phone = '555-1212';
  update acct;

 static void testMethod2() 
  Account acct = [SELECT Phone FROM Account WHERE Name='' LIMIT 1];
  System.assertEquals(null, acct.Phone);

  1. If a test class contains a test setup method, the test setup method executes first, before any test method in the class
  2. Multiple @testSetup methods are allowed in a test class, but the order in which they’re executed by the testing framework isn’t guaranteed
  3. 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.
  4. Available for API versions 24.0 and later
  5. If a fatal error occurs during the execution of a test setup method, such as an exception that’s caused by a DML operation or an assertion failure, the entire test class fails, and no further tests in the class are executed
  6. If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method
  7. 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

Amit Chaudhary


  1. Thanks Amit Blog is so Useful for Everyone

  2. Who to Write Test setup for insertion restricted objects

  3. please help Amit Chaudhary

  4. Perfect. I don't know if I see it well, but it seams to me that Test.startTest();-> stopTest() pair - which are normally only can be called once in a test method thanks to this @testSetup can be called twice, which is a great feature itself :)

  5. Thanks Amit a sincere fan of yours.