Skip to main content

SF-9202 · Concept · Easy

What does the @isTest annotation do?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

@isTest is the annotation that marks Apex code as test code. The platform treats @isTest-annotated classes and methods differently from regular Apex in several important ways.

What it does

  • Excludes the class from the 6 MB Apex code size limit on the org. Without @isTest, test code would push your production code limit toward the ceiling.
  • Identifies test entry points to the test runner. When you click “Run All Tests” or run sf apex run test, the platform looks for methods marked @isTest.
  • Prevents methods inside @isTest classes from being called from non-test code. A test class is sealed off from production Apex.

Where you put it

On a class

@isTest
private class AccountServiceTest {
    @isTest
    static void getActiveAccounts_returnsOnlyActive() {
        // ...
    }
}

Every test class needs this. Convention is private but the class can be public if needed (e.g. to share helper utilities across other test classes in the same package).

On a method

@isTest
static void someTest() {
    // ...
}

Methods inside an @isTest class still need their own @isTest annotation. Methods without the annotation are treated as helpers — they can be called from test methods but won’t be discovered as tests.

@isTest with parameters

The annotation accepts options:

@isTest(SeeAllData=true)        // Test sees all org data, NOT recommended
@isTest(IsParallel=true)        // Test can run in parallel with others
@isTest(OnInstall=true)         // Run only at managed package install time
OptionEffect
SeeAllData=trueTest can read existing org records. Discouraged — couples tests to org state.
IsParallel=trueAllows the test to run in parallel with others, speeding up test runs. Default since API 47.0 is not parallel.
OnInstall=trueRun during managed package installation. Skipped by default unless explicitly listed.

@isTest vs the older testMethod keyword

// Old style (still works, but deprecated)
static testMethod void oldStyle() { }

// Modern style
@isTest
static void newStyle() { }

The testMethod keyword is a leftover from API 23.0 and earlier. The modern equivalent is @isTest on the method. Both work; only the annotation supports parameters like SeeAllData.

On helper classes

If you write a TestDataFactory to create accounts, contacts, opportunities for tests, mark the class @isTest:

@isTest
public class TestDataFactory {
    public static Account makeAccount(String name) {
        return new Account(Name = name);
    }
}

This keeps the factory out of the production code size budget. It can still be called from test classes, but not from production Apex.

Common interview follow-ups

  • Why mark helper classes @isTest? — Keeps them out of the 6 MB Apex code limit; signals intent.
  • Can a test class be public? — Yes, but private is preferred. Public is needed when other packages must reference it (rare).
  • What happens if I forget @isTest on a test class? — Methods inside it still work as tests if individually annotated, but the class counts toward the production code size limit, and best practice is to mark both.

Verified against: Apex Developer Guide — IsTest Annotation. Last reviewed 2026-05-17 for Spring ‘26 release.