The TestSetup annotation
When we started the chapter defining the characteristics of our ideal tests, we stated that they should be quick to run. Whenever we are creating data within our tests, there is the possibility of us inadvertently slowing our tests down if we do not structure them properly. A common reason for slow tests within Apex is a repeated Data Manipulation Language (DML) action to set up test data that invokes the save order of execution. Using the @TestSetup
annotation can help reduce this, particularly when loading in larger volumes of data, as described here.
The @TestSetup
annotation defines a single method for a test class that is run once when the class is instantiated and creates test data for use by the methods within the class. Data is rolled back to this state after each test method is run and then expunged once the entire class has run.
I try to use the @TestSetup
method for defining all test data within any test class; however, it is especially useful...