I have a test method written by a third party that occasionally results in Apex CPU time limit errors. The part of the method that includes setting up a semi-large amount of data on multiple objects (Users, Accounts, Contacts) is the primary hog of the CPU time, and it would be impossible for a user to create all of it in a single transaction.

If I convert this to a @testSetup method in the class, do the CPU time limits apply to that method as well, even though it's not attempting to simulate user behavior and merely loading a bunch of test data? The documentation mentions that if a fatal error is encountered, it will roll back the transaction and cancel all test methods within the class, but doesn't mention specifically whether all limits apply within this context.

If a fatal error occurs during the execution of a test setup method, such as an exception that’s caused by a DML operation or an assertion failure, the entire test class fails, and no further tests in the class are executed.

1 Answer 1


Yes, the limits used by testSetup count against each unit test. One way to mitigate this effect is to use Test.startTest at the beginning of your testSetup method. The governor limits used between Test.startTest and Test.stopTest do not count against your unit test governor limits.

  • Just for clarification, you're suggesting to do something like the following, correct?: @testSetup static void loadData(){ Test.startTest(); //load data Test.stopTest(); } @isTest static void someTest(){ //query for records Test.startTest(); //perform unit test Test.stopTest(); } Commented Aug 23, 2017 at 18:24
  • @ToddSprinkel yes. That's the design we use. I also have a canary test that will warn me if this ever changes.
    – sfdcfox
    Commented Aug 23, 2017 at 18:29

You must log in to answer this question.

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