Skip to content

Apex Test Data Generator

Describe a record and get a test data factory or @TestSetup method in Apex.

Runs in your browser. Nothing is uploaded.

Record definition

5 fields

Start from an object

Fields
Generate

Apex test data

23 lines
/**
 * Test data factory for Account. Generated by Titrova.
 * Adjust the values for your org's required fields, validation rules and relationships.
 */
@IsTest
public class AccountTestDataFactory {
    public static List<Account> createAccounts(Integer count, Boolean doInsert) {
        List<Account> accounts = new List<Account>();
        for (Integer i = 0; i < count; i++) {
            accounts.add(new Account(
                Name = 'Test Account ' + i,
                Industry = 'Technology',
                AnnualRevenue = 250000,
                NumberOfEmployees = 50,
                Phone = '+1 415 555 0100'
            ));
        }
        if (doInsert) {
            insert accounts;
        }
        return accounts;
    }
}

Generated sample data may need adjusting for your org’s required fields, validation rules and relationships.

How to use the Apex Test Data Generator

  1. Enter the object’s API name, such as Account or Invoice__c, or pick one of the presets.
  2. 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.
  3. Choose the number of records and the kind of code: a factory class, a @TestSetup test class or an inline snippet.
  4. 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

Apex
@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 true or false.
  • Date becomes Date.newInstance(year, month, day) or an expression based on Date.today().
  • DateTime becomes DateTime.newInstance(year, month, day, hour, minute, second) or DateTime.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-7 and now become Date.today().addDays(…) and DateTime.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.Id and create that record in your test.
  • Picklist values are not checked against your org’s picklists.
  • The @TestSetup output uses the Assert class, available from API version 56.0. On older versions, replace it with System.assertEquals.
  • It writes test data, not test logic. The assertions for your own code are still yours to write.

Questions about the Apex Test Data Generator

For standard objects with no extra required fields or validation rules, usually yes. Most orgs have customisations, so expect to add a field or two. The generator cannot see your org’s rules.

Call it from a test method or a @TestSetup method, for example List<Account> accounts = AccountTestDataFactory.createAccounts(200, true);. Pass false to get records you can change before inserting them.

Duplicate rules, unique fields and matching rules can reject identical records. A different value per record avoids that and makes failures easier to trace.

Create the parent record first and reference it, for example AccountId = parentAccount.Id. Do not paste a real record ID: it will not exist in other orgs or in a fresh scratch org.

Create at least 200 in one of your tests. Triggers process records in batches of up to 200, and code that works for one record can fail at that size.

No. The code is generated in your browser from what you type. Object and field names never leave your device.