Test runs with SeeAllData=true
ID |
apex.correctness.unit_test_seealldata |
Severity |
high |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Test Quality |
Language |
Apex |
Description
Reports @IsTest(SeeAllData=true) on a test class or a test method, which gives the test access to the org’s real records instead of an isolated data set.
Rationale
A test that reads live data depends on rows nobody promised to keep: it passes in the org where it was written and fails in a new sandbox, in a scratch org, or the day someone deletes a record it silently relied on. Worse, the failure lands during a deployment, where the whole release stops for a test that never tested the code in the first place. The data set also differs between orgs, so the test asserts something different everywhere it runs.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Test runs with SeeAllData=true
@IsTest(SeeAllData=true)
private class LiveDataTest {
@IsTest
static void readsLiveAccounts() {
List<Account> accts = [SELECT Id FROM Account];
System.assertEquals(0, accts.size(), 'depends on the org');
}
}
Remediation
Remove SeeAllData=true and create the records the test needs, preferably in a @TestSetup method or a test-data factory. Where a genuinely unmockable standard object is involved - pricebook entries are the usual one - use Test.getStandardPricebookId() and the documented alternatives rather than opening the whole org.