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.
Here is an example of a private test class that contains two test methods.
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.
@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.
@isTest(isParallel=true) Annotation
- Considerations for the @isTest(isParallel=true) Annotation
-
- This annotation overrides settings that disable parallel testing.
- @isTest(SeeAllData=true) and @isTest(isParallel=true) annotations cannot be used together on the same Apex method.