Sub topic 4
Fresh governor limits between startTest and stopTest
This chapter assumes you can write a basic @IsTest class, use DML and SOQL in Apex, and know that governor limits cap what one transaction can do. You should also know that Test.startTest and Test.stopTest mark a test window, which the previous chapter covered.
What the reset does
A test method runs as one transaction. Your setup code, such as inserting records, spends part of that transaction's governor limits. Without help, a test that creates a lot of data could hit a limit before the code under test even runs.
Test.startTest() fixes this. Everything between startTest and stopTest gets its own fresh set of governor limits. The code under test starts with a full allowance, no matter how much the setup used.
The limits are split into two budgets:
- Setup code before
startTestuses the first budget. - Code between
startTestandstopTestuses the second, fresh budget. - After
stopTest, the code runs under the setup budget again.
Async work queued in the window runs at stopTest in its own context. The async context has the higher limits that real async jobs get, such as 200 SOQL queries instead of 100 in a future method or Queueable job.
Seeing the reset
Limits.getDmlStatements() returns how many DML statements the current context has used. The test below shows the count dropping back to zero when the window opens.
@IsTest
private class LimitResetTest {
@IsTest
static void limitsResetAtStartTest() {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 3; i++) {
accounts.add(new Account(Name = 'Limit ' + i));
}
insert accounts;
insert new Contact(LastName = 'Limit Contact', AccountId = accounts[0].Id);
Integer usedBefore = Limits.getDmlStatements();
Assert.areEqual(2, usedBefore);
Test.startTest();
Assert.areEqual(0, Limits.getDmlStatements());
insert new Account(Name = 'Inside window');
Assert.areEqual(1, Limits.getDmlStatements());
Test.stopTest();
}
}The two setup inserts count against the first budget. Inside the window the counter starts again from zero.
Why it matters for async tests
Setup data is often the heaviest part of a test. Suppose you insert records through triggers and flows that run many queries. Those queries all count against the setup budget. When you call your method after startTest, it should not inherit that usage, because in production it would run in its own transaction with its own limits.
The reset makes the test closer to production. It also means a test can pass even when setup plus the code under test would exceed the limits together. That is correct behavior, since they are separate transactions in production.
Limits you can still hit
The reset applies once per test method. Keep these points in mind:
- A test method can call
startTestandstopTestonly once. A secondstartTestcauses an error. - Work done in the window still counts against the window's budget. A loop of DML inside the window can still exceed 150 DML statements.
- Calling
stopTestends the window. Code after it returns to the first budget, which still holds the usage from setup. - Limits such as the Apex heap size and CPU time follow the same split.
Checking limit usage in the window
You can assert on limit usage to guard against regressions. For example, after the code under test runs, check that it stayed within a number you expect.
@IsTest
private class BulkSafetyTest {
@IsTest
static void queriesStayLow() {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accounts.add(new Account(Name = 'Bulk ' + i));
}
insert accounts;
Test.startTest();
List<Account> loaded = [SELECT Id FROM Account];
Integer queries = Limits.getQueries();
Test.stopTest();
Assert.areEqual(200, loaded.size());
Assert.areEqual(1, queries);
}
}Reading Limits.getQueries() before stopTest gives the usage of the window only, because the counter was reset at startTest.
Exercise
A test inserts 150 records in setup, which uses 1 DML statement. It then calls Test.startTest(), runs the code under test that uses 2 DML statements, and calls Test.stopTest(). What does Limits.getDmlStatements() return just before stopTest, and why?
Show answer
It returns 2. startTest gives the window a fresh limit budget, so the setup insert is not counted. Only the 2 DML statements from the code under test count in the window.
Exercise
Why can a test that puts its heavy setup code inside the startTest and stopTest window give misleading results?
Show answer
The setup then runs on the fresh budget, so the window no longer shows the limits the code under test would use on its own. Setup should run before startTest so that it uses the first budget.