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
chanti kchanti k 

Why we are using test.start and test stop test methods in test class to batch apex ?

Hi Experts,

i have little confuse ,Why we are using test.start and test stop test methods in test class to batch apex ? Any one can demonstrate on this  .      pls don't paste the link.

Regards,
Chanti
James LoghryJames Loghry
Test.startTest and Test.stopTest are used for asynchronous apex, like Batch Apex and Future calls.  Calling method(s) between Test.startTest and Test.stopTest ensure that any asynchronous transactions finish executing before Test.stopTest() exits.  This means, that if you use System.asserts after Test.stopTest, you'll have an accurate representation of your test.  Otherwise, if you called System.assert before Test.stopTest, or if you didn't use Test.startTest / Test.stopTest, then you couldn't gaurantee that the batch or future transaction was complete.
 
Amit GhadageAmit Ghadage
Hi chanti k,

We don't use Test.StartTest and Test.stopTest for batch apex only. We can use in  any test class.
The purpose of test.StartTest and Test.StopTest is to reset salesforce Governance limit. It is best practise to create dummy data for test classes and it requires DMLS and SOQL. 
So while executing actual apex classes to get full set of governance limit we use Test.startTest and test.stopTest and execute actual logic in between Test.starTest and Test.stopTest.

Best Regards,
Amit Ghadage.
VineetKumarVineetKumar
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 tostartTest and before stopTest is assigned a new set of governor limits.
So, every time you use the test.start and test.stop in your test method, new set of governor limits are assigned to the code executing between them. If you don't use them in your testmethods there may be chances of your test methods over shooting the set governor limits.
More details can be found in this salesforce documentation for the above.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
sfdc_beginner7sfdc_beginner7
I have question what if we dont use test.startTest and test.stopTest?
Dev_AryaDev_Arya
@sfdc_beginner7 @chanti, The startTest and startStop are not mandatory methods. They are needed when you need to execute the asynchronous code. Now why asynchronous code, as async code is supposed to be executed sometime when salesforce server resource is available (means free) to execute the code. But if you are running a test class, developer wants to see the results right away, so calling startTest makes sure that the async code is called and executed right away. similary, stopTest makes sure that all the async code is executed and all the expected records are processed, hence the code after the stopTest statement gets the expected result from the async method. This async method can be batch apex or future methods.

StartTest stopTest resetting the governor limits, is an additional advantage you get.  The primary goal of start and stopTest is execution of async code. As Async method query limits ideally should not be counted with your synchronous SQL queries in the test method, hence the code (SQL queries) between startTest and stopTest gets their own limits. 

Hope this clarifies your doubts. If you have further questions, feel free to ask. Otherwise mark this as green. :-P 

Cheers, Dev
GrifmangGrifmang
How governor limits are impacted if we test 20000 records using asynchronous apex inside the Test.StartTest() and Test.StopTest() block. I know that a different set of governor limits are applied to this block. But do these limits reset after every batch execution inside this block.
For example if you call queueable apex from a trigger then the trigger passes records in a batch of 200 records to the queueable class and the limits resets everytime it calls the queueable implemeted class because it is an async process (correct me if I am wrong here). If yes, does the same thing happen inside the test block ?
Thanks in advance.