function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurav Roy 15Saurav Roy 15 

What is the use of Test.startTest() and Test.stopTest() in a Test Class

Hello Everyone,
Can anyone please explain the significance of the Test.startTest() and Test.stopTest() in terms of asynchronous calls/apex ? I am aware of the governer limits being reset. 
Best Answer chosen by Saurav Roy 15
AbhishekAbhishek (Salesforce Developers) 
Hi Saurav,

As you have already know by now that Apex is governed by certain boundaries. The term coined for this is 'Governor Limits'.

There are two additional system static methods provided by Apex. These methods, Test.startTest and Test.stopTest, are used when testing governor limits. Or in other words, executing test scenarios with a larger data set.

These static methods allow a test method to separate the Apex resources and governor limits being used to prepare and initialize the dataset from the resources and limits used during the actual test execution.

Bottom Line - 

Governor limits are reset when the Test.startTest appears and the code between Test.startTest and Test.stopTest executes in a fresh set of governor limits (Context changes). Also, Test.stopTest appears, the context is again moved back to the original code.

Example -

you have 100 lines of code in your test class.

you have start test at 30 
you have stop test at 70

So the line of code from 30 -70 is the indifferent context of governor limits and
line 1-30/71-100 in different contexts. 

Total of 2 contexts here.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Regards,
Salesforce Support.

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Saurav,

As you have already know by now that Apex is governed by certain boundaries. The term coined for this is 'Governor Limits'.

There are two additional system static methods provided by Apex. These methods, Test.startTest and Test.stopTest, are used when testing governor limits. Or in other words, executing test scenarios with a larger data set.

These static methods allow a test method to separate the Apex resources and governor limits being used to prepare and initialize the dataset from the resources and limits used during the actual test execution.

Bottom Line - 

Governor limits are reset when the Test.startTest appears and the code between Test.startTest and Test.stopTest executes in a fresh set of governor limits (Context changes). Also, Test.stopTest appears, the context is again moved back to the original code.

Example -

you have 100 lines of code in your test class.

you have start test at 30 
you have stop test at 70

So the line of code from 30 -70 is the indifferent context of governor limits and
line 1-30/71-100 in different contexts. 

Total of 2 contexts here.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Regards,
Salesforce Support.
This was selected as the best answer
Saurav Roy 15Saurav Roy 15
Thank you, Abhishek.