9

I'm looking at the code coverage results from a recent run of all the tests in an org, and I'm not sure what all the colors represent.

The green and red seem fairly self explanatory:

  • Green - covered by one or more test cases
  • Red - not covered by any test cases

But what does the other color represent? I don't have the ability to describe that color, mustard maybe? Lets call it #5E5620.

VS Code Apex code coverage

Were those lines covered or not?

Note that all the test classes passed in the most recent run. So I don't think they represent failed test results.

Maybe something to do with partially covered lines? Although I'm not sure how that would work either. I don't believe the ApexCodeCoverage can represent that. Also, how do you partially cover a line like sourceRecords.add(devObj);?

For contrast, the developer console shows those lines as covered.

Developer Console code coverage


Investigation on the unknown color #5E5620 being somewhere between the known colors.

  • Green - Measured: #264C15 Source: #2D790B
  • Red - Measured: #8E3334 Source: #FD4849

enter image description here

It gets a bit complicated with the alpha/opacity being applied in the editor and the grey background. But it does seem plausible that the unknown color is in between the two covered and uncovered colors.


I've raised this as an issue in the GitHub repo - Code coverage showing unknown color that isn't covered or uncovered

7
  • 2
    Interesting. Only the green & red are defined in the code and the colorizer itself only seems to recognize covered & uncovered. Maybe a bug where two different runs are being consulted and both colors are being applied or something? Aug 15 '19 at 14:25
  • 1
    @ThomasTaylor I think you are onto something there with the colors being combined. The unknown one is somewhere in between the green and the red. Aug 15 '19 at 23:35
  • I didn't know that there was a colourised code coverage feature - is that part of a recent update or is that another tool that I'm not aware of? Aug 16 '19 at 0:12
  • 1
    @CasparHarmer It's changing so fast it's hard to keep up with what it does and doesn't do. A good problem I guess ¯\_(ツ)_/¯. The change log shows it in 45.7.0. I found it a bit fiddly to setup. Aug 16 '19 at 0:17
  • 1
    Definitely seems to be both of the colours being applied. Could it be that two different tests cover different parts, and for some reason it doesn't do the union of the two?
    – Matt Lacey
    Aug 16 '19 at 0:35
4

This seems probable, but I'm still very open to a better answer.


It's a bug in how the vscode extension is applying the covered and uncovered code coverage lines.

The styles for the covered and uncovered lines are currently defined in /src/codecoverage/decorations.ts as uncoveredLinesDecorationType and coveredLinesDecorationType.

These are then applied in /src/codecoverage/colorizer.ts. (Credit to @ThomasTaylor for pointing these locations out).

  const editor = window.activeTextEditor;
  if (editor) {
    editor.setDecorations(coveredLinesDecorationType, this.coveredLines);
    editor.setDecorations(uncoveredLinesDecorationType, this.uncoveredLines);
  }

So, first all the covered lines are colored green. Followed by coloring the uncovered lines red. As far as I can tell there isn't any consideration in building the uncoveredLines that those same line numbers already exist in coveredLines. That is something I needed to explicitly handle when I made a code coverage viewer. Lines can appear to be uncovered in some test methods that are covered by other tests.

The docs for setDecorations in the vscode docs aren't very clear on how multiple decorations are applied to the same line. It does say:

If a set of decorations already exists with the given decoration type, they will be replaced.

It certainly appears they the styles get combined to give a color somewhere between covered and uncovered.

It doesn't really explain why line 124 in the screenshot above doesn't show the same behavior. It definitely isn't hit by all the test cases for that method.

3
  • I suspect that might be a separate issue. I've seen the dev console highlight completely blank lines before now, its as if the coverage info gets out of sync with the source file version.
    – Matt Lacey
    Aug 16 '19 at 1:43
  • @MattLacey Are you referencing Line 124? As a precaution I tried clearing out all the existing code coverage and running the tests again. Just in case there was something hanging around from older test runs. It still produced the same result. Aug 16 '19 at 1:54
  • @DanielBallinger, It is a bug but nothing else. Please report. Aug 17 '19 at 14:59
0

It is a bug that I was able to replicate and correct with a workaround.

If you run your tests with code coverage enabled and certain lines weren't covered, those lines would be highlighted in Red. If you update your tests to cover those lines and then run your tests again, those previously Red lines would have the Green color applied over the Red giving the visual effect of both colors combined.

To correct this, you need to toggle off the code coverage in the footer to get rid of the colors from the previous tests. Run your tests again and then toggle the code coverage. You should see the correct colors now of Red and Green.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.