Before Salesforce introduced @testSetup annotation in 2015, I used to create common data in a method and call it in every test method.
Ex:
@isTest
private class clsAccountTest {
private static Account testAccount;
private static void createCommonData(){
testAccount = new Account(Name= 'Avinash', BillingPostalCode = '97214');
insert testAccount;
}
@isTest static void itShouldAskForZipCode(){
createCommonData();
//testAccount is accessible.
testAccount.BillingPostalCode = '12345';
update testAccount;
}
}
Now, with @testSetUp, it would look like:
@isTest
private class clsAccountTest {
private static Account testAccount;
@testSetup static void commonData() {
testAccount = new Account(Name= 'Avinash', BillingPostalCode = '97214');
insert testAccount;
System.Debug('****SOQL Queries issued****'+limits.getQueries()); //returns 5
System.Debug('****DML Statements issued****'+limits.getDMLStatements()); // returns 4. Issued 1 in testSetUp and it caused 3 more in Account trigger.
}
@isTest static void itShouldAskForZipCode(){
System.Debug('****SOQL Queries issued****'+limits.getQueries()); //return 5
System.Debug('****DML Statements issued****'+limits.getDMLStatements()); //return 4
createCommonData();
//testAccount is not avaialble, so you have to query for it.
testAccount = [SELECT Id, Name FROM Account WHERE Name = 'Avinash' LIMIT 1];
testAccount.BillingPostalCode = '12345';
update testAccount;
}
}
What do we gain by using this other than less execution time? The Number of SOQL statements, DML statements etc., generated in the testSetUp method are assigned to each and every test method. In fact, it forces us to use more queries as the static variables are not accessible in test methods.
In this answer, the user said that DML statements issued within the testSetUp method are not counted against governor limits, but the DML statements caused by it in the triggers will be counted. I tested this by inserting a record of an object that doesn't have any trigger code and that statement is not correct. Even the DML statements issued within the testSetUp method are counted against governor limits of each and every test method.
I'm wondering if there are any advantages other than less execution time.