Sub topic 5
Test data isolation and @TestSetup
This chapter assumes you can write a basic @IsTest class and run DML and SOQL in Apex. You should also know that governor limits apply per transaction.
Why tests start with an empty org
Apex tests do not see the data in your org by default. A test method starts with no accounts, contacts, or opportunities, even in a production org full of records. Every record the test needs must be created inside the test run.
Salesforce rolls back all data a test creates or changes when the test finishes. Tests never leave records behind, and one test cannot pollute another. This is what test data isolation means.
Two details matter:
- Setup objects such as
User,Profile, andRecordTypeare still visible, because they are configuration and not business data. That is why theSystem.runAsexample in this tutorial can query aProfile. @IsTest(SeeAllData=true)turns off the isolation and exposes real org data. Tests that use it break when the data changes. Avoid it unless the code depends on data you cannot create in a test.
Sharing data with @TestSetup
Many test methods in one class need the same records. Creating them in every method is slow and repetitive. A method annotated with @TestSetup runs once, before the test methods, and creates the shared records.
@IsTest
private class IsolationTest {
@TestSetup
static void setup() {
List<Account> accts = new List<Account>();
for (Integer i = 0; i < 3; i++) {
accts.add(new Account(Name = 'Setup Account ' + i));
}
insert accts;
}
@IsTest
static void deleteDoesNotLeakToOtherTests() {
delete [SELECT Id FROM Account WHERE Name = 'Setup Account 0'];
Integer remaining = [SELECT COUNT() FROM Account];
Assert.areEqual(2, remaining, 'One of the three setup accounts was deleted');
}
@IsTest
static void seesTheFullSetupData() {
Integer total = [SELECT COUNT() FROM Account];
Assert.areEqual(3, total, 'Each test starts with all three setup accounts');
}
}The two test methods can run in any order. The delete in the first method is rolled back, so the second method still finds three accounts.
Rules for @TestSetup methods
- The method must be
static, returnvoid, and take no arguments. - A class can have only one
@TestSetupmethod. - Test methods cannot use variables from the setup method. They must query the records again, because only the database state is shared.
- The setup method runs once, so its DML and queries count toward limits once, not once per test method.
- If the setup method throws an exception, every test method in the class fails.
- It cannot be used in a class annotated with
@IsTest(SeeAllData=true).
When to use it
Use @TestSetup for records most methods in the class read or modify. Create records inside a test method when only that method needs them, or when the record's field values are the point of the test. Putting too much in setup makes each test harder to understand, because the reader must scroll up to see what exists.
Common errors
| Symptom | Likely cause |
|---|---|
| `List index out of bounds` on a query result | The query filter does not match the setup data, or the data was never created |
| Test passes alone, fails with the whole class | The test depends on records from another test, which does not carry over |
| Every test in the class fails at once | The `@TestSetup` method threw an exception |
| Test passes in a sandbox, fails in production | The test uses `SeeAllData=true` and depends on org data |
Exercise
A class has a @TestSetup method that inserts 5 accounts. Test method A deletes all of them. Test method B runs afterward and counts accounts. What count does B see, and why?
Show answer
B sees 5. Salesforce rolls back each test method's changes, so every method starts with the data the @TestSetup method created, regardless of what earlier methods did.