1

How many records can be created in Test Setup method in test class

  • At the moment that's a very open question - can you add some more detail, how many do you need? It's a test class so there's really no harm in trying for yourself as well... – Girbot Jul 28 '18 at 10:32
  • I don't have any specific target count here,Just understand the limitation of the testsetup method. – SUresh Shettigar Jul 28 '18 at 10:34
  • Well it's the same as any apex transaction basically, so all the normal governor limits apply. If you have a lot of trigger/automation you might hit the cpu limit for example. – Girbot Jul 28 '18 at 10:37
  • You can create pretty much as many records as you need provided you don't exceed any SOQL, DML or other governer limits. Recognize the more records you create, the longer it will take for you unit test to complete. – crmprogdev Jul 28 '18 at 10:37
2

Salesforce didn't provide any limitation on how many data can be created on test setup method.

Just to be clear, test classes and test methods are for unit testing. So, to perform unit test cases atleast millions of data is not needed.

I have created thousands of records to test batch processes and it has worked well.

2

Here's a quick dirty test, aiming to create 10,000 records:

@isTest
public class testSetupInvestigate {
    @testSetup
    static void setupData(){
        List<Lead> leadsToInsert = new List<Lead>();
        List<Task> tasksToInsert = new List<Task>();
        for(Integer i=0;i<5000;i++){
            Lead l = new Lead(
                lastname = 'lastname'+i,
                company = 'company'+i
            );
            leadsToInsert.add(l);
            Task t = new Task(
                subject = 'TaskSubkject'+i
            );
            tasksToInsert.add(t);
        }

        insert leadsToInsert;
        insert tasksToInsert;
    }

}

Result?

enter image description here

My point being you can create thousands of records but you still need to be aware of the governor limits.

  • 1
    As an extra tip, you can (at time of writing) use Test.startTest() and Test.stopTest() inside of your @testSetup method to prevent the limits you've consumed in your setup method from counting towards the limits that your test method itself uses. Furthermore, you can still use Test.startTest() and Test.stopTest() as you normally would in your test method. – Derek F Jul 28 '18 at 12:49

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.