[SalesForce] Use of @IsTest versus testMethod and location of test methods in classes

Is there any advantage to using the older testMethod keyword to identify test methods or is it better to use the @IsTest annotation?

Does the location of these test methods have any significance? E.g. Either being included within the definition of the class being tested or defined in a separate class.

Best Answer

From http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm?SearchType=Stem

This class is defined using the @isTest annotation. Classes defined as such can only contain test methods. One advantage to creating a separate class for testing as opposed to adding test methods to an existing class is that classes defined with isTest don't count against your organization limit of 3 MB for all Apex code.

The advantage to using separate test classes is that they do not count against the limit of the total amount of apex in your org (although this is a soft limit and thus not a overly compelling reason).

You can also include non-test utility methods in classes marked as @isTest and (this is the compelling part) they do not need to be covered by tests themselves, and can only be called from inside of test methods! Very useful for data generation methods that are not needed in production code.