I am currently updating test classes in our org and it seems that most of them are exceeding the CPU Time Limit. They are using the @testSetup to create a lot of test data and it seems that these DML operations are triggering lots of rules. How can I avoid this?

EDIT>>>Here is a part of the test class, I tried running it while I removed all the codes in the test method. Still it shows CPU Time Limit Exceeded.

@testSetup static void setupTestData(){
    Product2 prod1 = new Product2();
    prod1.Name = 'Test Product 83202-1';
    prod1.ProductCode = 'TP 83202-1';
    prod1.SBQQ__ChargeType__c= 'One-Time';
    insert prod1;

    Id licenceRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Licence Account').getRecordTypeId();
    Id customerRT = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Customer Account').getRecordTypeId();

    Account a1 = new Account();
    a1.RecordTypeId = customerRT;
    a1.Name = 'Test Account 83202-C';
    a1.Type = 'Key Account';
    insert a1;

    Account a2 = TestClassObjectBuilder.createLicenceAccount(a1.id);
    a2.RecordTypeId = licenceRT;
    a2.Name = 'Test Account 83202-L';
    a2.Customer_Account__c = a1.Id;
    insert a2;

    Opportunity o = new Opportunity();
    o.Name = 'Test opportunity 83202';
    o.AccountId = a2.Id;
    o.CloseDate = System.today() - 5;
    o.StageName = 'Draft';
    insert o; 

    Pricebook2 testPB2 = new Pricebook2();
    testPB2.name = 'Standard Price Book';
    testPB2.IsActive = true;
    insert testPB2;

    SBQQ__Quote__c q = new SBQQ__Quote__c();
    q.SBQQ__Type__c = 'Renewal';
    q.SBQQ__Status__c = 'Draft';
    q.SBQQ__Primary__c = true;
    q.SBQQ__Account__c = a2.Id;
    q.SBQQ__Opportunity2__c = o.Id;
    q.SBQQ__StartDate__c = System.today() - 5;
    q.SBQQ__EndDate__c = System.today().addMonths(12);
    q.SBQQ__ExpirationDate__c = System.today().addMonths(12);
    insert q;


    Contract contr = new Contract();
    contr.AccountId = a2.Id;
    contr.Status = 'Draft';
    contr.StartDate = Date.today();
    contr.ContractTerm = 12;
    contr.Licence_Period_End_Date__c = Date.today().addDays(28);
    contr.SBQQ__RenewalQuoted__c = FALSE;
    contr.SBQQ__Quote__c = q.id;
    contr.SBQQ__Opportunity__c = o.id;
    insert contr;

    SBQQ__QuoteLine__c ql = new SBQQ__QuoteLine__c();
    ql.SBQQ__Quote__c = q.Id;
    ql.SBQQ__Product__c = prod1.Id;
    ql.SBQQ__Quantity__c = 1;
    ql.SBQQ__Number__c = 1;
    ql.SBQQ__PricingMethod__c = 'List';
    ql.SBQQ__ListPrice__c = 10;
    ql.SBQQ__CustomerPrice__c = 10;
    ql.SBQQ__NetPrice__c = 10;
    ql.SBQQ__SpecialPrice__c = 10;
    ql.SBQQ__RegularPrice__c = 10;
    ql.SBQQ__UnitCost__c = 10;
    ql.SBQQ__ProratedListPrice__c = 10;
    ql.SBQQ__SpecialPriceType__c = 'Custom';
    ql.SBQQ__ChargeType__c= 'One-Time';
    insert ql;

    Venue__c  venueRecord = TestClassObjectBuilder.createVenue();
    venueRecord.Name = 'test1';              
    Insert  venueRecord;   

    Affiliation__c  affilRecord = new Affiliation__c();
    affilRecord.Account__c = a2.Id;
    affilRecord.Venue__c = venueRecord.id;  
    affilRecord.Start_Date__c = system.today();    
    Insert  affilRecord;

    PriceBookEntry testPB = new PriceBookEntry();
    testPB.Pricebook2Id = test.getStandardPriceBookId();
    testPB.product2id = prod1.id;
    testPB.IsActive = true;
    testPB.UnitPrice = 100.0;
    insert testPB;

    Order testOrder = new Order();
    testorder.Accountid = a2.id;
    testorder.effectivedate = date.today();
    testOrder.SBQQ__Quote__c = q.id;
    testOrder.blng__BillingAccount__c = a2.id;
    testOrder.SBQQ__PaymentTerm__c = 'Net 30';
    testOrder.SBQQ__PriceCalcStatus__c = 'Completed';
    testOrder.status = 'Draft';
    testOrder.Pricebook2Id = test.getStandardPriceBookId();
    insert testOrder;  

    Order testOrder2 = new Order();
    testorder2.Accountid = a2.id;
    testorder2.effectivedate = date.today();
    testOrder2.SBQQ__Quote__c = q.id;
    testOrder2.blng__BillingAccount__c = a2.id;
    testOrder2.SBQQ__PaymentTerm__c = 'Net 30';
    testOrder2.SBQQ__PriceCalcStatus__c = 'Completed';
    testOrder2.status = 'Draft';
    testOrder2.Pricebook2Id = test.getStandardPriceBookId();
    insert testOrder2; 

    OrderItem testOrderItem = new OrderItem();
    testOrderItem.OrderId = testOrder.id;
    testOrderItem.PriceBookEntryId = testPB.Id;
    testOrderItem.SBQQ__QuoteLine__c = ql.id;
    testOrderItem.SBQQ__ChargeType__c = 'One-Time';
    testOrderItem.Quantity = 1.0;
    testOrderItem.UnitPrice = 100.00;
    testOrderItem.EndDate = date.today().addMonths(5);
    insert testOrderItem;

    testOrderItem.blng__OverrideNextBillingDate__c = null;
    testOrderItem.blng__BillThroughDateOverride__c = null;
    update testOrderItem;

    OrderItem testOrderItem2 = new OrderItem();
    testOrderItem2.OrderId = testOrder2.id;
    testOrderItem2.PriceBookEntryId = testPB.Id;
    testOrderItem2.SBQQ__QuoteLine__c = ql.id;
    testOrderItem2.SBQQ__ChargeType__c = 'One-Time';
    testOrderItem2.Quantity = 1.0;
    testOrderItem2.UnitPrice = 100.00;
    testOrderItem2.EndDate = date.today().addMonths(5);
    insert testOrderItem2;

    testOrderItem2.blng__OverrideNextBillingDate__c = null;
    testOrderItem2.blng__BillThroughDateOverride__c = null;
    update testOrderItem2;

    List<Order> listOrder = new List<Order>();

private static testMethod itShould{

  • 2
    @testSetup annotated method is running before every test method. it is possible, that not every test-method needs every test data, that is created in @testSetup. So leave in @testSetup only common for all test-methods data creation, remove from there non-common and spread it between test-methods via your own testdata-creation methods. Commented Oct 19, 2017 at 8:27
  • Hi Alex, thank you. Some test class are also having this problem even though it only has a few methods (2 methods).
    – jema
    Commented Oct 19, 2017 at 8:38
  • do you use Test.startTest(); Test.stopTest(); ? Commented Oct 19, 2017 at 9:18
  • Yep, I use it in the other method, except for @testSetup method. I tried to move the test data creation in one of the method and I not having an error anymore. Thanks Alex
    – jema
    Commented Oct 19, 2017 at 9:27
  • 1
    Seeing a portion of one of your test classes (the setup method + a test method that is running into this issue) would help. Temporarily having an empty test method (and running only that test, something you can do through the developer console) will give you an idea of how much of your limits it consumes. If you're creating more than a handful of records for each SObject that you need to run your test, you're probably wasting some of your limits (the Salesforce rec of inserting 200+ records is mainly just to ensure code is bulkified).
    – Derek F
    Commented Oct 19, 2017 at 12:36

1 Answer 1


Not only can you use Test.startTest() and Test.stopTest() in the regular test methods, but in addition to using it there, you can also use Test.startTest() and Test.stopTest() in the TestSetup method to gain an extra set of governor limits. I have found this technique to be invaluable to avoid governor limits in test classes.

Put differently, you can use Test.startTest() and Test.stopTest() in both the TestSetup method and the regular test methods at the same time to gain an extra set of governor limits during the tests.

You must log in to answer this question.

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