Newer Version Available
Loading Test Data
Simply, add the data in a .csv file, create a static resource for this file, and then call Test.loadData within your test method by passing it the sObject type token and the static resource name. For example, for Account records and a static resource name of myResource, make the following call:
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.
- text/csv
- application/vnd.ms-excel
- application/octet-stream
- text/plain
Test.loadData Example
- Create a .csv file that has the data for the test records. This
is a sample .csv file with 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:
- Click , and then New Static Resource.
- Name your static resource testAccounts.
- Choose the file you just created.
- Click Save.
- Call Test.loadData in
a test method to populate the test accounts.
1swfobject.registerObject("clippy.codeblock-2", "9"); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17@isTest 18private class DataUtil { 19 static testmethod void testLoadData() { 20 // Load the test accounts from the static resource 21 List<sObject> ls = Test.loadData(Account.sObjectType, 'testAccounts'); 22 // Verify that all 3 test accounts were created 23 System.assert(ls.size() == 3); 24 25 // Get first test account 26 Account a1 = (Account)ls[0]; 27 String acctName = a1.Name; 28 System.debug(acctName); 29 30 // Perform some testing using the test records 31 } 32}