Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Loading Test Data
- Add the data in a .csv file.
- Create a static resource for this file.
- Call Test.loadData within your test method and passing it the sObject type token and the static resource name.
1List<sObject> ls = Test.loadData(Account.sObjectType, 'myResource');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:
Test.loadData Example
- Create a .csv file that has the data for the test records. This sample .csv file has three
account records. You can use this sample content to create your .csv
file.
1Name,Website,Phone,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry 2sForceTest1,http://www.sforcetest1.com,(415) 901-7000,The Landmark @ One Market,San Francisco,CA,94105,US 3sForceTest2,http://www.sforcetest2.com,(415) 901-7000,The Landmark @ One Market Suite 300,San Francisco,CA,94105,US 4sForceTest3,http://www.sforcetest3.com,(415) 901-7000,1 Market St,San Francisco,CA,94105,US - Create a static resource for the .csv file:
- From Setup, enter Static Resources in the Quick Find box, then select Static Resources.
- Click New.
- Name your static resource testAccounts.
- Choose the file you created.
- Click Save.
- Call Test.loadData in
a test method to populate the test accounts.
1@isTest 2private class DataUtil { 3 static testmethod void testLoadData() { 4 // Load the test accounts from the static resource 5 List<sObject> ls = Test.loadData(Account.sObjectType, 'testAccounts'); 6 // Verify that all 3 test accounts were created 7 System.assert(ls.size() == 3); 8 9 // Get first test account 10 Account a1 = (Account)ls[0]; 11 String acctName = a1.Name; 12 System.debug(acctName); 13 14 // Perform some testing using the test records 15 } 16}