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
santhosh  konsanthosh kon 

What is the need of Test. starttest () method in Test classes?

Best Answer chosen by santhosh kon
Sai Ram ASai Ram A
Hi Santosh

The Limits methods return the specific limit for the particular governor, such as the number of calls of a method or the amount of heap size remaining.

There are two versions of every method: the first returns the amount of the resource that has been used in the current context, while the second version contains the word “limit” and returns the total amount of the resource that is available for that context. For example, getCallouts returns the number of callouts to an external service that have already been processed in the current context, while getLimitCallouts returns the total number of callouts available in the given context.

In addition to the Limits methods, use the startTest and stopTest methods to validate how close the code is to reaching governor limits.

The startTest method marks the point in your test code when your test actually begins. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.

The startTest method does not refresh the context of the test: it adds a context to your test. For example, if your class makes 98 SOQL queries before it calls startTest, and the first significant statement after startTest is a DML statement, the program can now make an additional 100 queries. Once stopTest is called, however, the program goes back into the original context, and can only make 2 additional SOQL queries before reaching the limit of 100.

The stopTest method marks the point in your test code when your test ends. Use this method in conjunction with the startTest method. Each test method is allowed to call this method only once. Any code that executes after the stopTestmethod is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.


Thank you
BLearn

All Answers

Waqar Hussain SFWaqar Hussain SF
using Test.starttest and test.stopTest we can avoid the governer limit of salesforce in test classes
Waqar Hussain SFWaqar Hussain SF
Please take a look to these articel, it really helps you.
it will surely solve your confusion. 
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
please let me know if you get the pont.
Sai Ram ASai Ram A
Hi Santosh

The Limits methods return the specific limit for the particular governor, such as the number of calls of a method or the amount of heap size remaining.

There are two versions of every method: the first returns the amount of the resource that has been used in the current context, while the second version contains the word “limit” and returns the total amount of the resource that is available for that context. For example, getCallouts returns the number of callouts to an external service that have already been processed in the current context, while getLimitCallouts returns the total number of callouts available in the given context.

In addition to the Limits methods, use the startTest and stopTest methods to validate how close the code is to reaching governor limits.

The startTest method marks the point in your test code when your test actually begins. Each test method is allowed to call this method only once. All of the code before this method should be used to initialize variables, populate data structures, and so on, allowing you to set up everything you need to run your test. Any code that executes after the call to startTest and before stopTest is assigned a new set of governor limits.

The startTest method does not refresh the context of the test: it adds a context to your test. For example, if your class makes 98 SOQL queries before it calls startTest, and the first significant statement after startTest is a DML statement, the program can now make an additional 100 queries. Once stopTest is called, however, the program goes back into the original context, and can only make 2 additional SOQL queries before reaching the limit of 100.

The stopTest method marks the point in your test code when your test ends. Use this method in conjunction with the startTest method. Each test method is allowed to call this method only once. Any code that executes after the stopTestmethod is assigned the original limits that were in effect before startTest was called. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously.


Thank you
BLearn
This was selected as the best answer