Tests code coverage in Visual Studio Code with C# and .Net Core

While writing tests for you code goes without saying, there is much to be said about code coverage of tests. How much of your code is actually covered by these tens of tests you just wrote? If you work in a TDD approach most probably you don't have such "mundane" questions. Of course you code is 100% covered. You did make sure every test failed before you added that line in your code to make it pass... and that conditional statement... or did you? While purists would argue there is no chance of no coverage I beg to differ. Let alone if you write your tests after the code is written... After such and such years and experience in the industry when we are nearing deadlines some things are thrown out the window. They shouldn't but they are. TL;DR tools help with coverage. In this case .Net Core Test Explorer with coverlet and Coverage Gutters.

Photo by Startup Stock Photos from Pexels

Coverlet to the rescue

As I blogged a while ago I have made myself a warm and cozy development environment for Visual Studio Code. One thing that popped to mind was tests code coverage. How can I see the coverage of my tests before they reach the CI pipeline? After a surprising brief search online I discovered a dotnet  core CLI tool called coverlet. One can use this tool to create a coverage file and feed that file to a coverage reporter or even in some of the CI tools out there. The tool mainly produces common formats for code coverage reporters. All one needs to do is install the coverlet tool and install a Nuget package to one's test project. Fortunately the tool's Github page has detailed installation and usage information.

.Net Core Test Explorer

So far so good. One can produce a textual representation of the coverage by running a command line tool. We are however developers and as such inherently lazy. I want the coverage to be updated every time I run my updated tests via the very very neat .Net Core Test Explorer extension for Visual Studio Code.

This extension discovers, runs all tests or single tests for debug and... test... via Code Lens as well as provide feedback on whether a single test has passed failed or is being skipped. One of its very useful features is the ability to feed the dotnet core test runner with arguments. Since we have coverlet installed we can now instruct the test runner to create the coverage (i.e. call coverlet), control the coverage file format and output location.

Part of  the .Net Core Test Explore settings
Part of  the .Net Core Test Explore settings

Coverage Gutters

This handy language agnostic extension will take coverage files in lcov format and paint the gutter next to the covered code in green orange or red at the hit of a keystroke. One side effect here is that that once the gutter is overlayed with the coverage one cannot set or remove breakpoints. There is, however, a workaround to this. Show the coverage inline... which actually is way better.

Part of the Coverage Gutters settings

The reason why this is handy is because in conditional statements where more than one condition is tested, it actually shows the condition missed by the test. Green color is for covered, orange for partially covered and red for not covered at all.

Coverage Gutters example
Coverage Gutters example

One caveat with this extension is that on Windows the "turn off coverage" keyboard shortcut does not work and cannot be set to the proposed combination. I opened in issue in Github and let the developer know. Unfortunately apart from setting a different keyboard shortcut there isn't anything else to do. I set mine to ctrl+shift+backspace.

I whipped up some code to demonstrate how these three extensions work together to help with coverage. You can find the code here.

Let me know in the comments below if you have found any more useful extensions for vscode! 


Comments

  1. This tool can be used very productively in order to create a file that will fully meet all the requirements.

    ReplyDelete
  2. Thank you for such a productive work and providing us with such a successfull project! Keep it up, sir! What else can we say?

    ReplyDelete
  3. Generally I do not read article on blogs, but I wish to say that this write-up very compelled me to try and do so! Your writing style has been amazed me. Thanks, quite nice post.

    Best Buy Geek Squad Appointment |
    Geek Squad Appointment Scheduling |
    Geek Squad Appointments At Best Buy |
    Make An Appointment With The Geek Squad |

    ReplyDelete

Post a Comment

Keep it clean, professional and constructive. Everything else including ethnic or religious references will be removed.

Popular posts from this blog

Visual Studio Code setup I use for C# development

Evaluating NDepend on current code base Part 1