Don’t forget your Apex Test Methods! | Salesforce Developers Blog

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.

Stay up to date with the latest news from the Salesforce Developers Blog

Subscribe