> ## Content Index
> Fetch the complete content index at: https://www.namastesalesforce.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Test data factory, the minimal version
- URL: https://www.namastesalesforce.com/snippets/test-data-factory-minimal/
- Published: 2026-04-25T09:00:00.000Z
- Updated: 2026-04-25T09:00:00.000Z
- Description: One class, builder-style defaults, no framework — the 80% of a test factory most orgs actually need.
- Author: Swarnil Singhai
- Tags: #snippet, #snippet-lang-apex, Apex, #Import 2026-09-03 19:13

```apex
@isTest
public class TestFactory {
    public static Account account() { return account('Acme ' + counter()); }

    public static Account account(String name) {
        return new Account(Name = name, Industry = 'Technology');
    }

    public static Contact contact(Id accountId) {
        return new Contact(FirstName = 'Test', LastName = 'Person ' + counter(),
                           AccountId = accountId,
                           Email = 'test' + counter() + '@example.com');
    }

    public static List<Account> accounts(Integer n) {
        List<Account> out = new List<Account>();
        for (Integer i = 0; i < n; i++) out.add(account());
        return out;
    }

    static Integer seq = 0;
    static Integer counter() { return ++seq; }
}
```

One class, builder-style defaults, no framework — the 80% of a test factory most orgs actually need.

## Gotchas

Return unsaved records and let the test decide when to insert — tests that need Ids call insert themselves, tests that don't stay fast. The counter keeps unique fields unique across a 200-record build.