How do governing limits work with @testSetup from Spring 15′ release

The new @testSetup method is covered pretty well by Jesse Altman in his: New Spring ’15 Feature: @testSetup article but as mentioned in the comments, there is a question about how this new feature works with the governing limits.

Does DML and SOQL statements count against the governing limits of my test methods? If so, how do they impact the governing limits of my overall test class?

Answer

As I have just rewritten our internal unit test design patterns I’ve tested this quiet thoroughly and believe I have a proper grasp on how @testSetup currently handles governing limits within the test class

In short, any DML statement within the context of @testSetup will not count against your overall test class limits but SOQL statements will.

For example:

@testSetup public static void virtualSandbox(){
    Account[] accts = new List<Account>();
    for(Integer i=0;i<3;i++) {
        Account a = new Account(Name='Acme' + i,BillingCity='San Francisco');
        accts.add(a);
    }
    INSERT accts;
    System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getQueries());

    List<Account> account_test = [SELECT Id, Name, QB_Company_File__c FROM Account];
    System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getQueries());
}

The above INSERT on Accounts will have no influence on your governing limit in of itself but the SOQL statement will count as 1.

NOTE: If your org has events that are triggered from that INSERT (which you likely do), THEN any DML or SOQL statement that proceeds from those are considered outside the context of @testSetup and will impact your overall governing limits for your test class.

As always these limits are reset within any test method when you invoke Test.startTest(); and Test.stopTest(); but this could affect how much data you can setup within your data setup method (for example when cascading dependencies like Contacts, which need Accounts).

Workflow Rules (WFR):

If you have a WFR on Leads that will INSERT an Activity based on certain criteria, then the INSERT of that Activity will not affect your limits but again if it triggers any DML/SOQL then expect it to impacting your limits

Workaround:

Because I have foundation test classes that cover a majority of the essential trigger context for every object I wanted to include as much data as necessary for certain test classes, but unfortunately I would run into limit issues quickly because of triggers. So thankfully, since we use Kevin O’Hara’s TriggerHandler Framework, we were able to implement his handy bypass() method which temporarily pauses all trigger activity within your TriggerHandler class

For example:

@testSetup public static void virtualSandbox(){
    TriggerHandler.bypass('AccountTriggerHandler'); // This will ignore the AccountTriggerHandler.cls that extends the TriggerHandler.cls framework

    Account[] accts = new List<Account>();
    for(Integer i=0;i<3;i++) {
        Account a = new Account(Name='Acme' + i,BillingCity='San Francisco');
        accts.add(a);
    }
    INSERT accts;

    TriggerHandler.clearBypass('AccountTriggerHandler'); // This will turn it back on as needed for the remainder of the test.
}

Hope this proves to be useful in someone, feel free to comment with any adjustments you believe are necessary to this explanation.

Attribution
Source : Link , Question Author : Xtremefaith , Answer Author : Xtremefaith

Leave a Comment