0

If someone asks, there are n Apex classes with varying code coverage, some less than 75 & some greater than.

What will happen while deployment?

So the answer would be -

(For Apex) all lines covered / all lines present = must be greater than 75%

Example - If while deploying to Prod, if there 3 classes :

  1. 80% ( Lines covered 80 / 100)
  2. 90% (Lined covered 900 / 1000)
  3. 0% (Lines covered 0 / 150)

So here on calculation it is : 980 / 1250 = 78.4% So this will get deployed to PROD correct ??

I just want to confirm is thing understanding of mine is correct for tackling such scenarios.

2 Answers 2

2

It depends on the value of TestLevel and any other classes that are already in production.

If TestLevel is set to RunLocalTests, and the average code coverage exceeds 75%, then your deployment succeeds, otherwise it fails. If you have exactly those three classes in your example, and you specify RunLocalTests, then the deployment would succeed. If you had many other classes, some with low coverage, the deployment could fail. The calculation is indeed "number of lines covered divided by total coverable lines of code must exceed 0.75 (75%)." Some code, like unit tests and comments, do not count for or against these calculations.

Contrariwise, if TestLevel is set to RunSpecifiedTests, then each class must independently have 75% coverage for the deployment to succeed, otherwise it fails. If you have exactly those three classes in your example deploying all at once, and you specify RunSpecifiedTests, then the deployment would fail. Note that classes that are not referenced in any test with RunSpecifiedTests do not contribute to this calculation. As long as the third class was already preexisting, and it was not touched by any unit test (e.g. neither of the first two classes referenced it), the deployment would succeed.

As you can see, success or failure will depend on the TestLevel used, as well as which classes are involved. Of course, if you also have triggers, you need to consider that those triggers must have some coverage (defined as 1%) when using RunLocalTests, but must have 75% when using RunSpecifiedTests, which might influence your decision on which mode to use. Note that RunLocalTests takes more time, but is effectively more lenient, while RunSpecifiedTests is faster, but stricter.

2
  • My understanding in short after reading your comment (for Apex classes) : 1. RunLocalTests - Depends on average / My above calculation rule 2. RunSpecifiedTests - Each individual class must be > 75%. @sfdcfox - I didn't completely get this part : ' Note that classes that are not referenced in any test with RunSpecifiedTests do not contribute to this calculation '. Can u pls help here?
    – Mohit K
    Commented Dec 16, 2021 at 11:50
  • 1
    @MohitKulkarni Taking your example classes, call them A, B, C, respectively, plus class D for unit test. With RunSpecifiedTests, if only A and B are called, then the deployment will pass, but if A, B, or D reference C in any way, then C with 0% coverage will fail the deployment.
    – sfdcfox
    Commented Dec 16, 2021 at 12:59
1

(For Apex) all lines covered / all lines present = must be greater than 75%

Apex Classes must have greater than or equals to 75% code coverage on deployment.

(For Trigger) Just need to have 1% coverage

This is a dangerous perspective on code coverage IMO. Although the platform will give a deployment error exception stating 1% code coverage is needed, this 1% ties in with your average code coverage across the organization.

This means that if your Trigger is covering 1% and deploys successfully, your next deployment is put at risk. You have just decreased the average code coverage for your organization.

Approaching code coverage in this manner will lead to very costly development efforts and an extremely unpleasant experience for the development/deployment teams.

As a best practice for code maintainability, product stability and overall experience, I recommend using a logic-less trigger approach forcing your business layer logic to a class such as a handler that interoperates with service layers. 75% code coverage becomes required at this point ensuring your business needs are tested appropriately for each and every deployment.

2
  • I understood logic-less triggers & handler classes logic. Now what I want to confirm, is if while deploying to Prod, if there 2 classes : 1. 80% ( Lines covered 80 / 100) 2. 90% (Lined covered 900 / 1000) 3. 0% (Lines covered 0 / 150) So here on calculation it is : 980 / 1250 = 78.4% So this will get deployed to PROD correct ??
    – Mohit K
    Commented Dec 16, 2021 at 10:09
  • You will need at least 1% on the last (3) artefact of code lines (assuming it is Trigger) and then it will deploy. If it is a class, you can deploy with 0% once these are the only 3 classes (unmanaged) and the average organisation code coverage sits at 78.4%.
    – TSmith
    Commented Dec 16, 2021 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .