We have a custom application created on the force.com platform which has a lot of unwanted SOQL Queries and DML Statements written in a poor format.

We are trying to stabilize the code now and looking for creating a test class which can run some valid business scenarios.
We have a test data creation class. When creating data in our test class, we are getting close to the soql and dml governor limit. Once the data creation is done, our actual test class which runs the scenarios fails on governor limits.

So my question is can I create 2 test methods, one where I can create test data in one context and another method to run my business scenarios which will have a different context?
That way I can get more limits and run my test classes

  • 2
    Including a Test.startTest() between the setup and code you are testing resets the limits. Also doing the setup in an @TestSetup method probably does the same thing (or may not). This helps somewhat but obviously only to some degree.
    – Keith C
    May 31, 2017 at 14:02
  • I think those limits consumed in @TestSetup still count against you.
    – Adrian Larson
    May 31, 2017 at 14:04
  • @KeithC Whatcha know, you're right. Test.startTest in testSetup prevents it from eating up your governor limits... TIL.
    – sfdcfox
    May 31, 2017 at 14:15
  • 2
    @AdrianLarson I dunno if it's a bug, but Test.startTest in \@testSetup gives you a new set of governor limits that won't affect your unit tests.
    – sfdcfox
    May 31, 2017 at 14:16
  • @sfdcfox That sounds like a bug. Still pretty cool and maybe they won't fix it.
    – Adrian Larson
    May 31, 2017 at 14:23

1 Answer 1


Set up your test data in @testSetup. To avoid using your test governor limits, start with Test.startTest():

@isTest class MyUnitTestClass {
  @testSetup static void testSetup() {
    // Do your DML operations here
  @isTest static void test() {
    // You have to re-query your records
    // Account a = [SELECT Id, Name... FROM Account]; etc...
    // Test your business logic here

Of course, this won't necessarily completely fix your problem, but it gives you a full extra set of governor limits for setting up your test data, which will probably help significantly.

You must log in to answer this question.

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