function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ckellieckellie 

Test Class Best Practice - 1 org or 1 per class/trigger?

I am in a project where i have to update all my test classes in my org with the new fields I am creating. A huge time commitment. What is best practice for apex code? Should I configure one class where I have create my test class records or create the test data in each test class? This affects 50+ test classes in my org.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
As a best practice you should always create test data in seperate class.
This is recommended as it gives flexibity to modify your code and reusability of code so that if there are any changes to your app/objectsyou will have to do minimal modifications in you test codes.

Thanks,
N.J
ckellieckellie
How do I create test data in a separate class, and how do I attach the class in each test class?

Thank you
James LoghryJames Loghry
ckellie, 
The idea is you would create a public class (you can also mark it with the isTest annotation so that it doesnt count against your code coverage).  The public class would have public static methods called "loadData" or "loadAccounts" or whatever.  These methods would simply insert your mock data.  You could return the records too if you need to get their IDs or whatever.

In your test class, you would call it like so:

static testmethod void myFirstTest(){
    TestUtils.loadData(); //inserts accounts, contacts, whatever
    Test.startTest();
    myClass.myMethod();
    Test.stopTest();
    //Requery data here if you need to.
    System.assertEquals(expected,actual);
}

Another option you could use (I haven't gone this route myself) is using the load from CSV functionality.  See this document for more information: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_load_data.htm
Karan Khanna 6Karan Khanna 6
create a test utility class and in methods create records of your objects. then in your actual test class call the methods so that you dont have to create data again in test class.

follow this: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_utility_classes.htm