Newer Version Available
IsTest Annotation
Classes and methods defined as isTest can be either private or public. Classes defined as isTest must be top-level classes.
This 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}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, and can't be called by a non-test request.. 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, including 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 by default 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 and its data access is unchanged. 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, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the testmethod keyword.
- The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level. However, using isTest(SeeAllData=false) on a method doesn’t restrict organization data access for that method if the containing class has already been defined with the isTest(SeeAllData=true) annotation. In this case, the method will still have access to all the 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}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, will be 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, won't be 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}