Test.startTest and Test.stopTest
By the end of this lesson, you'll be able to:
- Explain what Test.startTest() and Test.stopTest() do to governor limits
- Use them to separate test-data setup from the code actually being tested
- Recognize their role in testing asynchronous code
Prerequisites: "Creating Test Data"
A fresh set of governor limits for the code under test
@isTest
static void bulkInsertStaysWithinLimits() {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accounts.add(new Account(Name = 'Test Account ' + i));
}
Test.startTest();
insert accounts; // governor limits reset here — this DML gets a fresh budget
Test.stopTest();
System.assertEquals(200, [SELECT COUNT() FROM Account]);
}
@isTest— marks the test method.static void bulkInsertStaysWithinLimits() {— the test.List<Account> accounts = new List<Account>();— the data setup begins.for (Integer i = 0; i < 200; i++) {— building 200 records.accounts.add(new Account(Name = 'Test Account ' + i));— everything here doesn't count toward the governor limits Module 25 covered.}— closes the loop.- (blank line)
Test.startTest();—Test.startTest()marks the boundary.insert accounts;— governor limits reset here — this DML gets a fresh, separate budget.Test.stopTest();— marks the end.- (blank line)
System.assertEquals(200, [SELECT COUNT() FROM Account]);— confirms all 200 were saved.}— closes the test method.
This lets a test set up substantial data without that setup competing for the same limits as the actual code being verified.
Why this matters for asynchronous code
Test.startTest();
someService.queueAsyncWork(); // enqueues a Queueable job (a later module covers these)
Test.stopTest();
// by this point, the async job has actually finished running
List<Result__c> results = [SELECT Id FROM Result__c];
System.assertEquals(1, results.size());
Test.startTest();— marks the boundary.someService.queueAsyncWork();— enqueues a Queueable job (a later module covers these).Test.stopTest();—Test.stopTest()has a second important effect: any asynchronous Apex enqueued sincestartTest()is forced to run synchronously, to completion, before this line returns.- (blank line)
// by this point, the async job has actually finished running— spells out that guarantee.List<Result__c> results = [SELECT Id FROM Result__c];— safe to check the async job's output now.System.assertEquals(1, results.size());— without this guarantee, a test asserting on the async job's results might run before that job had actually finished, in a real (non-test) execution.
Exercise
Wrap this test's DML in Test.startTest()/Test.stopTest(), keeping the data setup loop before the boundary.
Show hint
Test.startTest() goes right before the insert statement.
Test.startTest and Test.stopTest Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Test.startTest() and Test.stopTest() mark the boundary between setting up test data and running the code actually being tested — resetting governor limits at that boundary and forcing async code to finish before assertions run.