Activity Forums Salesforce® Discussions What is the use of @testsetup annotation in a test class?

  • Prachi

    Member
    August 20, 2018 at 12:57 pm

    Hi Avnish,

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

    Thanks.

  • Parul

    Member
    September 12, 2018 at 4:11 am

    The use of @testsetup annotation in a test class:

    Use this annotation if you want to create test data once and use it in all test methods of your class. Therefore, you don't need to recreate the data again.
    Records created in a test setup method are rolled back at the end of test class execution.
    @testSetup methods are executed first in the class before any test methods.

     

    Thanks

  • shariq

    Member
    September 15, 2018 at 1:11 pm

    Hi,

    To add more into this -

    You can have only one test setup method per test class. 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.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos