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?

up vote 25 down vote accepted

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.

  • 5
    Worth noting that once you call stopTest the governor limits are reset to what they were before you called startTest. This can make querying back for data after the fact a bit tricky, if you're in an org that consumes a lot of limits. – Adrian Larson Jun 23 '15 at 17:01

First you want to do all of your setup, so anything inserts or updates that you need to do as preperation for the actual test should happen before the Test.startTest()

Then you place the code that you are testing inside the Test.startTest() and Test.stopTest() tags.

Finally you put your asserts after the Test.stopTest() tag.

So if you are testing a single method on an VF Page Controller you would create any necessary records. Set the page, and initialize the Controller, call any other methods that might be necessary before the method you are testing.

Then when you are ready to test your method set the Test.startTest() then the method you need to test and then the Test.stopTest()

Updated

The tags are not required for testing. You could leave them out and there would be no issues.

What do the tags do? According to Salesforce Documentation...

startTest()

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

Usage

You can also use this method with stopTest to ensure that all asynchronous calls that come after the startTest method are run before doing any assertions or testing. 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.

stopTest()

Marks the point in your test code when your test ends. Use this method in conjunction with the startTest method.

Usage

Each test method is allowed to call this method only once. Any code that executes after the stopTest method 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.

So they basically give you a seperate context for your test as far as governor limits go. Anything before and after the tags use one set of limits, anything in-between get their own set of limits. They also ensure that any asynchronous code that you are testing will finish before any assertions you make in your code.

Salesforce's intention is for you to use them with Asynchronous calls, and when you are worried about hitting limits do to setup and what not.

Again you don't always need them, I however use them in all my tests as a kind of symantic marker to let myself, and those who might come after me looking at my code, to know exactly what I'm attempting to test.

  • Doug, Why is that there are no Test.startTest() and Test.stopTest() methods used in this example? developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – javanoob Jun 23 '15 at 15:52
  • 1
    @javanoob the tags aren't required or needed really in most areas. Also I've found Salesforce documentation to be lagging in several areas. I've updated my answer with perhaps a better explanation to answer your question. – dBeltowski Jun 23 '15 at 16:06

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.