function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alexander the KeeperAlexander the Keeper 

Which is more convenient / faster / scalable ? Test.loadData vs Test data Factory

Hello, 

I have a situation when we have about 50 different test classes, and each of them has from 1 to 10 test methods.

As far as I understand, for speed is better to have a testSetup method in classes with a significant number of test methods, because data not added but refreshed for each method.

In my case, more than 50% of tests use the same necessary data, so I was wondering how to optimize its creation.

Do anybody know the pros/cons sheets for Test.loadData(CSV) and Test Data Factory.

I understand basics and documentation, but maybe someone has real-life experience with using both of them and can provide its feedback?
Raj VakatiRaj Vakati
Its not an apple to apple comparision but i will suggest its dependence on the use case  ..

Test data Factory advantages 
  1. Common test utility classes are public test classes that contain reusable code for test data creation.
  2. Public test utility classes are defined with the isTest annotation, and as such, are excluded from the organization code size limit and execute in test context. They can be called by test methods but not by non-test code.The methods in the public test utility class are defined the same way methods are in non-test classes. They can take parameters and can return a value. The methods should be declared as public or global to be visible to other test classes. These common methods can be called by any test method in your Apex classes to set up test data before running the test. While you can create public methods for test data creation in a regular Apex class, without the isTest annotation, you don’t get the benefit of excluding this code from the organization code size limit.
This is an example of a test utility class. It contains one method, createTestRecords, which accepts the number of accounts to create and the number of contacts per account. The next example shows a test method that calls this method to create some data.

 
@isTest
public class TestDataFactory {
    public static void createTestRecords(Integer numAccts, Integer numContactsPerAcct) {
        List<Account> accts = new List<Account>();
        
        for(Integer i=0;i<numAccts;i++) {
            Account a = new Account(Name='TestAccount' + i);
            accts.add(a);
        }
        insert accts;
        
        List<Contact> cons = new List<Contact>();
        for (Integer j=0;j<numAccts;j++) {
            Account acct = accts[j];            
            // For each account just inserted, add contacts
            for (Integer k=numContactsPerAcct*j;k<numContactsPerAcct*(j+1);k++) {
                cons.add(new Contact(firstname='Test'+k,
                                     lastname='Test'+k,
                                     AccountId=acct.Id));
            }
        }
        // Insert all contacts for all accounts
        insert cons;
    }
}

Test.loadData(CSV) Advanatges 


Using the Test.loadData method, you can populate data in your test methods without having to write many lines of code. 

Useful if you wanted to pass the read-only fields to test class like created date etc ..


The Test.loadData method returns a list of sObjects that correspond to each record inserted.
You must create the static resource prior to calling this method. The static resource is a comma-delimited file ending with a .csv extension. The file contains field names and values for the test records. The first line of the file must contain the field names and subsequent lines are the field values. To learn more about static resources, see “Defining Static Resources” in the Salesforce online help.
Once you create a static resource for your .csv file, the static resource will be assigned a MIME type. Supported MIME types are:
text/csv
application/vnd.ms-excel
application/octet-stream
text/plain
List<sObject> ls = Test.loadData(Account.sObjectType, 'testAccounts');

 
Alexander the KeeperAlexander the Keeper
Thank you, Raj.
I'm sorry, but you didn't provide use cases when one is better than another.
And I don't see why it's not apple to apple comparison.
If you have skills to create CSV and to create test data factory, then I don't see a big difference.

What comparison I meant is something like that:

-------------------------------------------------------------------- Data.load   Factory

Test data can be modified without additional deployments    V     X
pass the read-only fields to test class like created date         V     X
contain reusable data for test data creation                           V     V
excluded from the organization code size limit                      V     V

I'm actually cannot think of a situation when Factory will be better than Data.load right now.