+ Start a Discussion
Chandu007Chandu007 

Can i write single test class for multiple classes

Hi,

I have multiple classes related to single object. some test classes covering code coverage upto 40 to 50 which doesn't have separate test class. my question is should i write test class again for every class or can use(call all classes) same test class for mutiple classes. Please help.
Best Answer chosen by Chandu007
Andrew GAndrew G
Hi Chandu

Technically, a single Test Class can cover various / multiple classes / triggers depending on how it is written.

Some consider best practice though to have one test class per Apex Class or Trigger, with a naming convention of 
MyAccountClass
MyAccountClass_Test

That convention is usually so that the test classes for a given method / class can be quickly found when using your IDE.

If you wish to do a single Test Class for multiple classes / triggers, then I would strongly suggest that in the both the Class and the Test Class you make annotations that indicate the relation between the classes for ease of future reference.
example
// this Banana Class is tested by test Class AlphabetSoupTest
and vice verse
//this test class Alphabet Soup provides coverage for ABCclass, DoReMiClass and BananaClass

Regards
Andrew

All Answers

Andrew GAndrew G
Hi Chandu

Technically, a single Test Class can cover various / multiple classes / triggers depending on how it is written.

Some consider best practice though to have one test class per Apex Class or Trigger, with a naming convention of 
MyAccountClass
MyAccountClass_Test

That convention is usually so that the test classes for a given method / class can be quickly found when using your IDE.

If you wish to do a single Test Class for multiple classes / triggers, then I would strongly suggest that in the both the Class and the Test Class you make annotations that indicate the relation between the classes for ease of future reference.
example
// this Banana Class is tested by test Class AlphabetSoupTest
and vice verse
//this test class Alphabet Soup provides coverage for ABCclass, DoReMiClass and BananaClass

Regards
Andrew
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Chandu,
Greetings to you!

- Yes, you can write but Salesforce has some limits which will not allow to run all the classes from one test class, You can run only a few classes.
- There's a number of tests that you can run in a 24-hour window. There are also concurrent limits related to using too many resources at once. Practically speaking, this means that a "run all tests" should ideally be spread out over time; run single tests whenever possible, and reserve run all tests for a system-check for code coverage, etc. You can also run into this if multiple users try to run all tests at once. Only one person at a time should be performing a run all test run. In ordinary cases, run only single tests.


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
Ajay K DubediAjay K Dubedi
Hi Chandu007,

Yes, you can write one test class for multiple test class but it will not under comes in best practice.
Using one single class usually isn't practical (there is a 1MB limit per class, which you can easily exceed
when you're pushing 2MB of live code), using one test class per live class or trigger may also be counterproductive.

You can follow this link more help:
https://www.janbasktraining.com/blog/test-classes-apex-salesforce/
http://blog.shivanathd.com/2013/11/Best-Practices-Test-Class-in-Salesforce.html 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Chandu007Chandu007
Thanks All for responding. your answers helped me. I will write separate test classes for every class & trigger.