How to use the Apex Test Data Generator
- Enter the object’s API name, such as
AccountorInvoice__c, or pick one of the presets. - Add a row for each field: its API name, its type and a sample value. Mark text and email fields as unique to add the loop counter.
- Choose the number of records and the kind of code: a factory class, a
@TestSetuptest class or an inline snippet. - Copy the Apex or download it, then adjust it for your org’s required fields and validation rules.
Why tests need their own data
Apex tests run in isolation from the records in your org unless you opt out with SeeAllData=true, which is discouraged. Each test class therefore builds the accounts, contacts or custom records it needs. Typing new Account(Name = …) again and again is tedious, and scattered copies all break together when someone adds a required field.
Three ways to organise test data
Test data factory
An @IsTest class with static methods such as createAccounts(Integer count, Boolean doInsert). Every test class calls it. When the data model changes, you update one method. This is the most reusable option and the default output here.
@TestSetup method
A method that runs once before the test methods in a class. The records it inserts are visible to each test method, and each test starts from that same state. Use it when several tests in one class need the same records.
Inline snippet
A list and a loop, or a single record, to paste into a method you already have. Handy for a quick test or for Anonymous Apex in a scratch org.
Example output
@IsTest
public class ContactTestDataFactory {
public static List<Contact> createContacts(Integer count, Boolean doInsert) {
List<Contact> contacts = new List<Contact>();
for (Integer i = 0; i < count; i++) {
contacts.add(new Contact(
LastName = 'Contact ' + i,
Email = 'test.contact' + i + '@example.com'
));
}
if (doInsert) {
insert contacts;
}
return contacts;
}
}How each field type is written
- String, Phone, Picklist and Email become quoted text with quotes and backslashes escaped.
- Integer and Decimal are written as plain numbers after checking they are valid.
- Boolean becomes
trueorfalse. - Date becomes
Date.newInstance(year, month, day)or an expression based onDate.today(). - DateTime becomes
DateTime.newInstance(year, month, day, hour, minute, second)orDateTime.now(). - Lookup ID becomes a reference to an Apex variable, or a quoted ID if you enter one (with a warning, because IDs differ between orgs).
Adjusting the code for your org
Treat the output as a first draft. Run it, read the first error, and add what your org insists on: a required custom field, a record type, a value that satisfies a validation rule. Because the factory is one method, you only make each fix once.
Features and limitations
What it does
- Three outputs: a reusable test data factory, a test class with
@TestSetup, or a snippet to paste into an existing method. - Ten field types, each written as the correct Apex literal or constructor, such as
Date.newInstance(2026, 2, 28). - Relative dates:
today,today+30,today-7andnowbecomeDate.today().addDays(…)andDateTime.now(). - Unique values: the loop index is appended to text and inserted into email addresses.
- Bulk-friendly code: records are built in a list and inserted with one DML statement.
- Input checks for API names, numbers, dates, emails and lookups, with a specific message for each problem.
What it does not do
- It does not know your org. Required fields, validation rules, duplicate rules, record types and triggers may need extra values.
- Lookup fields need a parent record. Reference a variable such as
parentAccount.Idand create that record in your test. - Picklist values are not checked against your org’s picklists.
- The
@TestSetupoutput uses theAssertclass, available from API version 56.0. On older versions, replace it withSystem.assertEquals. - It writes test data, not test logic. The assertions for your own code are still yours to write.