Activity Forums Salesforce® Discussions What is the use of testSetup in Salesforce?

  • Hariom Chaudhary

    Member
    August 9, 2019 at 11:24 am

    @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.

    Fore more information visit:

    https://developer.salesforce.com/forums/?id=9060G0000005X0yQAE

  • Laveena

    Member
    August 9, 2019 at 11:25 am

    Hi Saddam,

    Test setup methods are defined in a test class, take no arguments, and return no value.

    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.

    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. Because data isolation for tests is available for API versions 24.0 and later, test setup methods are also available for those versions only.

    Thanks

    • This reply was modified 5 years, 4 months ago by  Laveena.
  • Nikita

    Member
    August 9, 2019 at 11:25 am

    Hi Saddam,

    Apex has introduced a new method in Test class known as “Test Setup”. It needs to be marked with @testSetup annotation in Test class. Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.
    One Test class can have only one @testSetup method. These test setup methods are implicitly invoked before each test methods.
    These methods can be used to create test records specific to Test class. It helps to keep Test classes isolated and independent of helper or utility classes. If an error occurs in the setup method then the entire test class fails. If the non-test method is called from the setup method then no code coverage is calculated for non-test method.

    • This reply was modified 5 years, 4 months ago by  Nikita.
  • Prachi

    Member
    August 9, 2019 at 11:29 am

    Hi Saddam,

    We use @testSetup because methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.

    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.

    Thanks.

    • This reply was modified 5 years, 4 months ago by  Prachi.

Log In to reply.

Popular Salesforce Blogs