1

I am a bit new to apex coding and has banged my head against the code coverage for a week. I made a trigger, helper, rest callout etc that work just fine. I wrote a bunch of unit tests for the code too.

When I check my code coverage in the developer console after the tests has been run all lines are marked as blue (I guess this mean they are covered) but the code coverage is listed as very poor, like between 1 and 50% depending on method.

Eventually I got fed up and had a peek in the setup/Custom Code/Apex Classes and Apex triggers. here my trigger and helper etc show as having 100% coverage, all of the methods.

Why is it so, that the developer console report such bad results and the setup give me full pott?

Best regard Daniel

2
  • In my experience code coverage calculations are restricted to the Apex invoked through tests executed in the most recent test execution. Thus, if you only ran a single test class or even a single test, this will show just the coverage related to that execution. It should also be noted that if you allow parallel test execution then the test coverage reports can be incorrect since coverage details are not always merged from the parallel executions so some detail can be lost and you see a lower coverage than expected.
    – Phil W
    Mar 31, 2021 at 14:56
  • Thanks for the info, I'll keep it in mind. Apr 1, 2021 at 6:25

3 Answers 3

1

Sometimes, the code coverage will get out of sync. To get proper coverage to show up in Developer Console, follow these steps:

  • Click Test > Clear Test Data
  • Click Test > Run All Tests

As the tests finish, the coverage will be calculated. Once you do this, you should be good to go for a while. I typically do this once every few days to make sure my coverage is properly updated.

The only way to guarantee the coverage is correct after making changes is to do the above. I recommend doing this only just before you're ready to deploy; running individual tests for classes and triggers you're working on should give you a decent indication of what your coverage is for those specific items.

1
  • I actually did this at one point but it did not improve the code coverage in the developer console :( Apr 1, 2021 at 6:16
0

Actually the developer console log is the easiest way to check code coverage and execution errors as well, were you checking as well the Overall Code Coverage Tab in the bottom right of the window! and in the Tests tab, does the folder of your execution has a green mark in its left, that would mean that the test is successful, and that would be the first thing to check, the code can be covered but if the test fails the coverage is not effective

PS: I don't have years of experience and I am sharing what I know, I hope this helped a bit clear things up for you !

1
  • Maybe this is the answer on my question. My tests do get the green check on them but in the "Overall Code Coverage" I have a lot of junk (my dev environment) that has 0% coverage though the trigger and classes I am working on now has 100%. It is a bit surprising that the upper left code coverage tab seem so inconsistent, for some of my classes like the callout class it show 100% but for the trigger and trigger handler it is really poor. In the "Overall Code Coverage" to the bottom right corner they have 100%. Thanks for the tip Apr 1, 2021 at 6:20
0

I've experienced something very similar to what you've described; here's what I've learned:

The developer console seems to retain its code coverage for a given file until one of two things happens:

  1. The file is re-saved
  2. The test is re-run
  3. A different test is run, which touches the file

So for me, typically there is a very short window of time where I can view the total coverage of a file, due to my constant saving habits. In order to truly answer what's going on with you, we'd probably need a step-by-step breakdown of everything you're doing from the point of saving a file, through testing and observing the code coverage. Dev Console is very finicky in terms of when it will give you what information.

1
  • 1
    Tank you for the tip, I'll keep it in mind. Apr 1, 2021 at 6:24

You must log in to answer this question.

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