When to use Test.startTest?

I am new to salesforce development and I am quite not sure on when to use the below two methods:

Test.startTest() and Test.stopTest()

1) Do i need to surround all my insert, update,SOQL and DML statements with startTest and stopTest?

The documentation also says that

Marks the point in your test code when your test actually begins.

2) Does it mean all the System.assert statements should be surrounded with startTest() and stopTest()?

3) Is it OK if I add Test.startTest() at the start of my test method and Test.stopTest() at the end of my test method?

Marks the point in your test code when your test actually begins. Use this method when you are testing governor limits.

4) Documentation says use this to test the governor limits. I thought all the callouts, SOQL and DML statements in test methods do not count towards the governor limit. What exactly is testing governor limits??

5) Another thing, Is it mandatory to use these methods? What happens if I don’t use? The example on this page(https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm) does not use these methods and the tests still pass. I can think of only one scenario, where we are forced to use these methods is when testing any asynchronous code so that the processing is finished before asserting the data.

My apologies if this question does not make any sense but I am just trying to understand when to use these two methods?


Test.startTest() and Test.stopTest() exist primarily to allow you to reset the governor limits within the context of your test execution and to be able to test asynchronous methods. These two statements cannot be called more than once within a testMethod. They are not required to be used but in some situations may be critical. Asynchronous (@future) calls made during the test are also finalized when you call Test.stopTest() allowing you to test the results of asynchronous behavior in your code.

Given a scenario where you need to execute numerous queries and a large number of DML rows in order to set up the data for the actual code being tested, it might be impossible for you to run your test without these statements.

For instance in this fictional scenario, if during the setup of your test you needed to execute 99 SOQL queries and insert 9,999 records to seed the org with the data your code required for proper testing, if Salesforce did not offer a mechanism to reset the governor limits the code which you are testing would only have room for one more SOQL query and one more record in a DML statement before it would hit one of those two limits (100 queries and 10,000 records processed by DML statements respectively) and throw an exception.

In the above scenario, if you were to call Test.startTest() after your 99 queries were complete and your 9,999 rows were DML’d – the transaction limits within your test would be back to zero and at that point the code which you are testing would be running in a context that more closely resembles a single transaction’s limits in real life. This mechanism allows you to “ignore” the work that had to be done to set up the test scenario.

Source : Link , Question Author : javanoob , Answer Author : Mark Pond

Leave a Comment