A recent addition to the Apex documentation library at developer.force.com is the Introduction to Apex Test Methods. This introductory article is intended mainly for developers who are learning Apex or who may not be familiar with Apex test method requirements. Here's a snippet from the abstract:

To facilitate the development of robust, error-free code, Apex Code requires the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit tests are written in Apex Code and annotated with the testMethod keyword.

It is important to understand that test methods are required to deploy Apex to a production environment. They are also required if your code is going to be packaged and placed on Force.com AppExchange. The test methods must provide at least 75% code coverage. Think of a test method as 'Apex code that tests other Apex code.' Code coverage is calculated by dividing the number of unique Apex code lines executed during your test method execution by the total number of Apex code lines in all of your trigger and classes.

It is also important to not think of test methods as simply a requirement by the Force.com platform. It should not be an afterthought. Writing test methods should be a critical part of Force.com development and they are required to ensure your success. Test methods provide test automation which can enable greater QA efficiency. It also provides an automated regression testing framework to validate bug fixes or future development enhancements.

If you are just getting started with Apex or want to learn more about how to write Apex test methods, please continue with the rest of the Introduction to Apex Test Methods.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS