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
  4. Geek Squad Chat is a well-known tech service provider company in the world. Our experts offer support for all types of technical problems faced in computers, laptops and smartphones. They even provide solutions for home appliances, TV, smart wearable and home theatre. We help our customers in setup, installation, repair, uninstallation, replacement, damage and other issues. Our dedicated support team is always available at your service and assures you for the quick resolutions. Book geek squad chat today and get your device fixed without wasting much time.
    geek squad chat |
    geek squad chat |
    geek squad chat |
    geek squad chat |

    ReplyDelete
  5. A charming conversation is worth remark. I believe that you should distribute more on this topic, it probably won't be an untouchable issue yet for the most part individuals don't examine these issues. To the following! Kind respects!!
    best interiors

    ReplyDelete
  6. Tbilisi | Tbilisi, Georgia, Tbilisi, Georgia, T-2021 - Vitanium Art
    Tbilisi is a titanium welding Georgian city that columbia titanium pants was built 사이트 추천 by the Georgian government. It has been named the city titanium undertaker of Tbilisi in 2021 dewalt titanium drill bit set by

    ReplyDelete
  7. Main Features of Microsoft Office 2007 Crack Full Version Free Download. Completely interactive user interface; Supports XML, PDF and XPS file formats.MS Office 2007 Crack

    ReplyDelete
  8. We really appreciate what you do. My favourite blog
    Native Instruments Massive

    ReplyDelete
  9. Thank you for taking the time and sharing this information with us. Ziyyara offers customized CBSE Board Home Tuition online, with experienced and certified tutors, providing personalized attention to each student.
    For more info contact +91-9654271931 or visit Online Tuition for CBSE Board

    ReplyDelete
  10. I really like your post. I found it very helpful. Please keep posting such kind of valuable post. Dive into our meticulously curated list of over 1000+ Classifieds Submission Sites.
    visit Classified submission sites

    ReplyDelete
  11. Very awesome post! I really like that and very interesting content. Keep it up a good job. Ziyyara Edutech's AS Level online courses and tutoring are here to provide the support you need.
    Book A Free Demo Today visit AS level courses

    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

NUnit Console Runner and NLog extension logging for your tests

Applying an internal forums system in the company culture - thoughts