We have a custom application created on 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 now which can run some valid business scenarios. We are facing issues with the test class where our test data class which creates test data is getting closer to soql and dml limit governor exception. Once the test data class is done with creation of test data, our actual test class which runs the scenarios fails on governor limits.

So my question is can i create 2 test methods where i can create test data in one method with a different context and create another method to run my business scenarios which will have a different context? This 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 '17 at 14:02
  • I think those limits consumed in @TestSetup still count against you. – Adrian Larson May 31 '17 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 '17 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 '17 at 14:16
  • @sfdcfox That sounds like a bug. Still pretty cool and maybe they won't fix it. – Adrian Larson May 31 '17 at 14:23

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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