Search code examples
unit-testingsalesforceapexapex-codesalesforce-lightning

Salesforce Tests for Org


Should every APEX class have its own test class?

Currently dealing with tests in an org where 80% of APEX tests are under one test class which does not seem to be the best option.


Solution

  • I would say that yes indeed, having all the org's tests in one single class is not really something recommended. Some of the drawbacks that I see:

    • Big classes increase dependency meaning that is more likely that several developers work on the same class, which increases complexity
    • When one big class contains all the test methods it is harder to functionally "scope" your tests. When an independent class contains test methods for a certain class, it is easier to understand what is trying to be tested and to further extend the class coherently
    • By using different classes you can make better use of the @testSetup annotation (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm) because only one method per class can be annotated with the @testSetup annotation

    Those are some of the reasons (among others for sure) why you would prefer to split your test methods in different classes.