10

The concept of testing org is still very new to me. I don't think I fully understand it. But can I've more than one StartTest() and StopTest() in my test class as my test requires many cases to ensure my codes have coverage?

@isTest
private class TestSFParSched_Class{
   //Case1
   static testmethod1 void test() {
      test.StartTest();
      test.StopTest();

   }
   //Case2
   static testmethod2 void test() {
      test.StartTest();
      test.StopTest();

   }

}

Thanks

1 Answer 1

24

You can have one StartTest / StopTest per test method.

Each test method is executed independently. They may be on the same class but they share nothing and do not see side effects from each other.

You can have many test methods in any test class 99.9% of the time. The 0.1% allows for features like Queueable interface that currently require their own class.

There are two main use cases for StartTest / StopTest.

  1. StartTest resets all governor limits. In this way you can set up your test data and still clearly determine the impact of your functionality on relevant governor limits.
  2. StopTest forces asynchronous calls to execute. Using this method will allow you to guarantee an asynchronous call or batch has run.

There are also stylistic benefits; for AAA style tests (Arrange, Act, Assert) it is a great way to identify where the Act happens. This helps a reader to know what you intend to test.

3
  • I disagree with your last assertion. In addition to asynchronous functions and indicating intent, it allows limit testing, as Test.startTest resets all governor limits.
    – Adrian Larson
    Commented Feb 16, 2015 at 19:28
  • @AdrianLarson I agree, definitely it would be great to see wider use of these markers. Can you propose an edit that conveys this? Commented Feb 16, 2015 at 19:46
  • 3
    The Queueable bug has been fixed. There goes your 0.1%.
    – sfdcfox
    Commented Feb 17, 2015 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .