Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The results are in! See what nearly 90,000 developers picked as their most loved, dreaded, and desired coding languages and more in the 2019 Developer Survey.

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.

history | excerpt history