Activity Forums Salesforce® Discussions What is @TestSetup annotation?

  • saloni gupta

    Member
    July 31, 2017 at 11:54 am

    hi shubham,

    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() {

    }

  • Shaharyar

    Member
    August 12, 2017 at 3:19 am

    Hello Shubham,

    As you know that, Initially we had to create test data in every test method, because every test method is considered a different transaction with its own governor limits. But Salesforce has introduced @TestSetUp annotation for test class method. You can write a method in test class, with @TestSetUp annotation applied, and create all your common test data in this method.

    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. I will show this in testMethod2 of below example.

    @isTest
    private class TestSetupMethodExample {
    //Below is a method with @testsetup annotation, the name can be anything like setup(), oneTimeData(), etc.
    @testSetup static void setup() {
    // Create common test accounts
    List<Account> testAccts = new List<Account>();
    for(Integer i=0;i<2;i++) {
    testAccts.add(new Account(Name = 'TestAcct'+i));
    }
    insert testAccts;
    }

    @isTest static void testMethod1() {
    // Here, we will see if test data created in setup method is available or not, Get the first test account by using a SOQL query
    Account acct = [SELECT Id FROM Account WHERE Name='TestAcct0' LIMIT 1];
    // Modify first account
    acct.Phone = '555-1212';
    // This update is local to this test method only.
    update acct;

    // Delete second account
    Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
    // This deletion is local to this test method only.
    delete acct2;

    // Perform some testing
    }

    @isTest static void testMethod2() {
    // The changes made by testMethod1() are rolled back and
    // are not visible to this test method.
    // Get the first account by using a SOQL query
    Account acct = [SELECT Phone FROM Account WHERE Name='TestAcct0' LIMIT 1];
    // Verify that test account created by test setup method is unaltered.
    System.assertEquals(null, acct.Phone);

    // Get the second account by using a SOQL query
    Account acct2 = [SELECT Id FROM Account WHERE Name='TestAcct1' LIMIT 1];
    // Verify test account created by test setup method is unaltered.
    System.assertNotEquals(null, acct2);

    // Perform some testing
    }

    }

     

  • Parul

    Member
    September 18, 2018 at 8:19 pm

    Hi

    @testSetup: For most unit tests in Salesforce, we need to generate some test data. And for different test methods within one single test class, the required test data set is usually very close. In order to serve for this purpose, Salesforce has introduced TestSetup annotation. The data generated in TestSetup will be persisted for every test method within the test class to use. And the data will only be rolled back after the execution of the whole test class completes.Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.
    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos