Skip to main content

A unit test is code that is written and executed to test a specific portion/unit of source code. Unit tests are class methods with the testMethod modifier or @isTest annotation and are usually placed in separate test classes annotated with the @isTest annotation. Code that is executed by unit tests is considered covered. At least 75% of an organization's codebase must be covered by unit tests and every trigger must have at least 1% unit test coverage.

Questions related to the design and development or any other area of unit tests should be tagged with the unit-test tag.

Unit tests typically require setting up data on which the code under test operates. As of API version 24 (Spring '12) unit test methods do not have access to the organization's data by default, with the exception of setup and metadata objects. However, the organization's data can still be accessed by explicitly indicating that intent by using the @isTest(SeeAllData=true) annotation.

Once the data is set up the unit of code under test is executed and then typically System.assert assertions about the results of the execution are made.

To learn more about unit tests in Salesforce see the following.