2

I am working on a new project where the client's pre-existing production code has a low coverage of 72% thus not allowing me to deploy any work done in the Sandbox.

Error:

Code Coverage Failure Your code coverage is 72%. You need at least 75% coverage to complete this deployment.

Does anyone have recommendations as to how to increase code coverage?

Edward Nigma
  • 45
  • 1
  • 5

1 Answers1

6
  1. Compile all classes in production
  2. Run all your unit tests (local ones, no need to run tests that come with managed packages)
  3. Go to Developer Console, Query Editor, tick at the bottom the Tooling API checkbox
  4. Run this query

    SELECT ApexClassorTrigger.Name, NumLinesCovered, NumLinesUncovered
    FROM ApexCodeCoverageAggregate
    ORDER BY NumLinesUncovered DESC
    LIMIT 10
    

It should give you a good idea which classes/triggers are least covered. Some of these will be quick wins, time spent on creating/improving their tests will give you best results in oveall coverage. I mean it's better to spend 1h fixing class that has 60 out of 100 lines covered than class that has 2 out of 4 covered. Work in sandbox till you're > 75%

(there's a chance your sandbox is outdated and somebody created validation rules, required fields etc straight in production without deploying... that's why I asked to compile & run all tests in prod)

If there are classes/methods that aren't used anymore and it'd be safe to delete them - you can't do it with changeset, you need a special destructive deployment. For now you could comment them out and deploy that version. Just check if this is beneficial for you (I mean of course it's good to get rid of old code, easier maintenance... but if it happens to be well covered with tests you'll shoot yourself in the foot)

Add the created/updated test classes to changeset and you should be able to deploy it to prod.

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • When I run this I get the exception: INVALID_TYPE: sObject type 'ApexCodeCoverageAggregate' is not supported. – Enkode Mar 04 '21 at 01:50
  • 2
    From dev console? make sure the "Tooling API" checkbox at bottom of query editor is selected. From REST APIs (for example workbench.developerforce.com -> Utilities -> REST Explorer) and then `/services/data/v50.0/tooling/query?q=SELECT+ApexClassorTrigger.Name,+NumLinesCovered,+NumLinesUncovered+FROM+ApexCodeCoverageAggregate+ORDER+BY+NumLinesUncovered+DESC+LIMIT+10` – eyescream Mar 04 '21 at 18:16