Newer Version Available

This content describes an older version of this product. View Latest

isTest Annotation

Use the @isTest annotation to define classes and methods that only contain code used for testing your application. The @isTest annotation can take multiple modifiers within parentheses and separated by blanks.

The testMethod keyword is now deprecated. Use the @isTest annotation on classes and methods instead. The @isTest annotation on methods is equivalent to the testMethod keyword.

Note

Classes and methods defined as @isTest can be either private or public. Classes defined as @isTest must be top-level classes.

Classes defined with the @isTest annotation don't count against your organization limit of 6 MB for all Apex code.

Note

Here is an example of a private test class that contains two test methods.

1@isTest
2private class MyTestClass {
3
4   // Methods for testing
5   @isTest static void test1() {
6      // Implement test code
7   }
8
9   @isTest static void test2() {
10      // Implement test code
11   }
12
13}
Here is an example of a public test class that contains utility methods for test data creation:
1@isTest 
2public class TestUtil {
3
4   public static void createTestAccounts() { 
5      // Create some test accounts
6   }
7
8   public static void createTestContacts() {
9      // Create some test contacts
10   }
11
12}

Classes defined as @isTest can't be interfaces or enums.

Methods of a public test class can only be called from a running test, that is, a test method or code invoked by a test method. Non-test requests cannot call public methods.. To learn about the various ways you can run test methods, see Run Unit Test Methods.

@isTest(SeeAllData=true) Annotation

For Apex code saved using Salesforce API version 24.0 and later, use the @isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization. The access includes pre-existing data that the test didn’t create. Starting with Apex code saved using Salesforce API version 24.0, test methods don’t have access to pre-existing data in the organization. However, test code saved against Salesforce API version 23.0 and earlier continues to have access to all data in the organization. See Isolation of Test Data from Organization Data in Unit Tests.

Considerations for the @isTest(SeeAllData=true) Annotation
  • If a test class is defined with the @isTest(SeeAllData=true) annotation, the annotation applies to all its test methods. The annotation applies if the test methods are defined with the @isTest annotation or with the (deprecated) testMethod keyword.
  • The @isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, if the containing class has been annotated with @isTest(SeeAllData=true), annotating a method with @isTest(SeeAllData=false) is ignored for that method. In this case, that method still has access to all the data in the organization. Annotating a method with @isTest(SeeAllData=true) overrides, for that method, an @isTest(SeeAllData=false) annotation on the class.
  • @isTest(SeeAllData=true) and @isTest(isParallel=true) annotations cannot be used together on the same Apex method.
This example shows how to define a test class with the @isTest(SeeAllData=true) annotation. All the test methods in this class have access to all data in the organization.
1// All test methods in this class can access all data.
2@isTest(SeeAllData=true)
3public class TestDataAccessClass {
4
5    // This test accesses an existing account. 
6    // It also creates and accesses a new test account.
7    static testmethod void myTestMethod1() {
8        // Query an existing account in the organization. 
9        Account a = [SELECT Id, Name FROM Account WHERE Name='Acme' LIMIT 1];
10        System.assert(a != null);
11        
12        // Create a test account based on the queried account.
13        Account testAccount = a.clone();
14        testAccount.Name = 'Acme Test';
15        insert testAccount;
16        
17        // Query the test account that was inserted.
18        Account testAccount2 = [SELECT Id, Name FROM Account 
19                                WHERE Name='Acme Test' LIMIT 1];
20        System.assert(testAccount2 != null);
21    }
22       
23    
24    // Like the previous method, this test method can also access all data
25    // because the containing class is annotated with @isTest(SeeAllData=true).
26    @isTest static void myTestMethod2() {
27        // Can access all data in the organization.
28   }
29  
30}
This second example shows how to apply the @isTest(SeeAllData=true) annotation on a test method. Because the test method’s class isn’t annotated, you have to annotate the method to enable access to all data for the method. The second test method doesn’t have this annotation, so it can access only the data it creates. In addition, it can access objects that are used to manage your organization, such as users.
1// This class contains test methods with different data access levels.
2@isTest
3private class ClassWithDifferentDataAccess {
4
5    // Test method that has access to all data.
6    @isTest(SeeAllData=true)
7    static void testWithAllDataAccess() {
8        // Can query all data in the organization.      
9    }
10    
11    // Test method that has access to only the data it creates
12    // and organization setup and metadata objects.
13    @isTest static void testWithOwnDataAccess() {
14        // This method can still access the User object.
15        // This query returns the first user object.
16        User u = [SELECT UserName,Email FROM User LIMIT 1]; 
17        System.debug('UserName: ' + u.UserName);
18        System.debug('Email: ' + u.Email);
19        
20        // Can access the test account that is created here.
21        Account a = new Account(Name='Test Account');
22        insert a;      
23        // Access the account that was just created.
24        Account insertedAcct = [SELECT Id,Name FROM Account 
25                                WHERE Name='Test Account'];
26        System.assert(insertedAcct != null);
27    }
28}

@isTest(OnInstall=true) Annotation

Use the @isTest(OnInstall=true) annotation to specify which Apex tests are executed during package installation. This annotation is used for tests in managed or unmanaged packages. Only test methods with this annotation, or methods that are part of a test class that has this annotation, are executed during package installation. Tests annotated to run during package installation must pass in order for the package installation to succeed. It is no longer possible to bypass a failing test during package installation. A test method or a class that doesn't have this annotation, or that is annotated with @isTest(OnInstall=false) or @isTest, is not executed during installation.

This example shows how to annotate a test method that is executed during package installation. In this example, test1 is executed but test2 and test3 is not.

1public class OnInstallClass {
2   // Implement logic for the class.
3   public void method1(){
4      // Some code
5   }
6}
1@isTest
2private class OnInstallClassTest {
3   // This test method will be executed
4   // during the installation of the package.
5   @isTest(OnInstall=true)
6   static void test1() {
7      // Some test code
8   }
9
10   // Tests excluded from running during the
11   // the installation of a package.
12
13   @isTest
14   static void test2() {
15      // Some test code
16   }
17
18   static testmethod void test3() {
19      // Some test code
20   }
21}

@isTest(isParallel=true) Annotation

Use the @isTest(isParallel=true) annotation to indicate test classes that can run in parallel. Default limits on the number of concurrent tests do not apply to these test classes. This annotation makes the execution of test classes more efficient, because more tests can be run in parallel.

This annotation overrides settings that disable parallel testing.

@isTest(SeeAllData=true) and @isTest(isParallel=true) annotations cannot be used together on the same Apex test method.

Note