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
Mathew Andresen 5Mathew Andresen 5 

best way to create test data


I'm trying to get my head around OO best practices, and how to really implement it correctly.  So for example with my test data, I'm thinking that it should really be in one class, and then called as needed by all my other classes yes?  And if so, what is the best way to strucutre it.  I'm thinking in general there should be a seperate method for each type of data I would need for each object, but I'm not really sure.

Here is what I have so far.  Thoughts on best way to do this?
public class CreateTestData {
    List<Account> acctList = new List<Account>();
    List<Opportunity> oppList = new List<Opportunity>();
    List<Client_Plan__c> clientList = new List<Client_Plan__c>();
    List<Client_Plan_Product__c> clientProdList = new List<Client_Plan_Product__c>();
    List<Tasting__c> tasteList = new List<Tasting__c>();
    List<Tasting_Product__c> tasteProdList = new List<Tasting_Product__c>();
    public List<Account> CreateAccount(String testName, Integer count) {
        for (Integer i = 0; i < count; i++) {
            Account acct = new Account(name = (testName + i) );
        return acctList;
    public list<Opportunity> CreateOpportunity(String testName, Integer count) {
        if(acctList==NULL) {
            CreateAccount(testName, count);
        for (Account acct :acctList) {
            for(Integer i = 0; i <count; i++) {
                Opportunity opp = new Opportunity(Name = (testName+count), AccountId =;
        return oppList;
    public list<Client_Plan__c> CreateClient(String testName, Integer count) {
            if(acctList==NULL) {
            CreateAccount(testName, count);
        for(Integer i = 0; i <count; i++) {
            Client_Plan__c plan = new Client_Plan__c(Name = (testName+count), Account__c = acctList[i].id);
        return (ClientList);



Best Answer chosen by Mathew Andresen 5
Cyrus TalladenCyrus Talladen
I think what you really mean is to encapsulate your test logic inside a test class by using Test Utility classes.  see docs here:

And you're right each action should be in a method and each class should follow the Single Responsibility Principle.

Cyrus T