Activity › Forums › Salesforce® Discussions › What is @testSetup annotation? When you will use it?
Tagged: Annotation, DML Error Notification, SOQL Query, Test Method, Test Setup, TestSetup Annotation
-
What is @testSetup annotation? When you will use it?
Posted by Prachi on August 2, 2018 at 2:03 PMWhat is @testSetup annotation? When you will use it?
Anjali replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
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:- We can use only one test setup method per class.
- Test setup method will execute first in the test class.
- If any DML error occurs in this test setup method, the rest of the method not executed.
- If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.
Thanks.
- [adinserter block='9']
-
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
-
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.
- Method marked with @TestSetUp annotation executes before any testMethod.
Log In to reply.