Often when my team writes Apex unit tests, we have to set up a large amount of records to get all of the data in place to appropriately test a method. With all of the automation in place in our Org, we often hit our governor limits (usually SOQL limits). This week we discovered startTest
and stopTest
which can help alleviate this problem.
Call Test.startTest()
after all of the test setup code and Test.stopTest()
before the test assertion code. The code that runs within those two methods runs in its own context with its own set of governor limits. After stopTest
is called, the context returns to the same as before startTest
.
Read the Salesforce documentation on startTest
and stopTest
.