I have many custom objects with lots of relationships and i want to create some test data for my test classes. For example, i have the Objects Account Opportunity Basket Product Configuration Product Definition Attribute

What is the best way in order to create test data for those Objects. I am thinking to do the following:

  1. Create test data in csv files for each object as independent records
  2. Upload the files as static resources in my sandbox
  3. Inside my test Apex class load the Accounts static resource
  4. Then, load the opportunities static resource and after loading them, set the Accounts as parents and update the opportunities.
  5. Load the bsaket data and set the opportunity id field to the respective opportunities.

Is this a best practice? Is there maybe a "quicker" road to this?

2 Answers 2


Salesforce provides the Test.loadData method that leverages static resource CSV files to load data for specified SObject types which will do what you need.

Part of the problem you will face if you do try to use CSV static resources is making relationships between these objects without introducing an external ID field just for testing.

I would say that we don't take this approach and instead have explicit code (marked as @IsTest) to handle creating test data (this being one or more "test data factory" with varying responsibilities in terms of data creation). It is more verbose but keeps all test-specific materials in one place - test apex - and simplifies creating hierarchies of objects.

  • 3
    I find that using a test data factory class is more effective that using static resource files,with methods to create accounts, contacts, opportunities, cases, etc where the relationships can be built by the methods.
    – Dave Humm
    Sep 14, 2020 at 9:22
  • 1
    Exactly my point.
    – Phil W
    Sep 14, 2020 at 9:30
  • Thank you very much guys! Sep 14, 2020 at 9:51

If you want to generate testing data for standard and custom SObjects, use Forceea data factory (https://github.com/Forceea/Forceea-data-factory). Forceea uses a descriptive language (Dadela), which makes your data generation easy, flexible and powerful. It has very advanced capabilities and many methods to make your life easier.

For example, to generate serial numbers from 1, stepping 1.25, with 2 decimals, you use: .setDefinition(Account.MyField__c, 'serial type(number) from(1) step(1.25) scale(2)')

Another example: to create random accounts with Industry = "Banking" or "Services" from previously created accounts of the group "BigAccounts", you use: .setDefinition(Contact.AccountId, 'random lookup(Account) field(Industry) value(Banking,Services) group(BigAccounts) source(forceea)')

With Forceea you can have static, serial or random data. You can copy values, you have automated handling of picklist dependencies, variables, and Templates which help a lot when you want to create a DataFactory class for complex data structures.

You must log in to answer this question.

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