Code coverage in .net core apps

It was good but not great YET cos I aim for 100%

As developer, we are fully responsible to deliver the quality code and a good coverage of the source code is, in my opinion, a good indicator how well the business logic is covered with tests.

Since .net core 1.0, I have been searching for a good nudges package for my MS Tests and xUnit Tests but unfortunately, there was none. The question of how I could ensure the code I wrote is well tested was actually nagging me for a while. Just to clarify, there are actually add-ins for VS2017 from JetBrains that could be a good candidate for the job but I was a bit hesitated to invest on them as it’s not for commercial purpose. I mainly explored on .net core on my own interest and the love of .NET cross platform framework.

Fortunately, since March 2018, the coverlet.msbuild was being released and it did it job well. Shayne Boyer blogged about it here and so did Scott in his own blog. It was pretty a big things and I am feeling lucky being part of the community.

Without further delay, I created a POC repository to illustrate the capability of the coverage. The link to my git hub can be found here.

Just a quick summary on my POC of coverlet.msbuild and a few tricks to get it works on the ‘legacy’ projects

Note 1: the latest version of coverlet (2.6.0) is not compatible .NET.Test.sdk and xUnit, hence it’s tricky to figure out which version is backward compatible. Example of those are working fine:

Stable working versions

Note 2: I enable Coverage Gutters so that I can see the highlighted on the source code with tests coverage. It is VS Code extensions. Coverage Gutters will look for specific file format of lcov hence when running test, the coverage should be outputted into that lcov format. I used following command to achieve it

dotnet test /p:CollectCoverage=true
/p:CoverletOutputFormat=lcov
/p:CoverletOutput= ../my.tests/lcov

Note 3: I use dotnet watch on the test project during the development so that any change on the source code as well as the tests will trigger the test run automatically. It helps on ensure small incremental change is made. I used following command to do the watch

dotnet watch –project .\my_test_project\ test
/p:CollectCoverage=true
/p:CoverletOutputFormat=lcov
/p:CoverletOutput=../my.tests/lcov

Hope it be helpful for those whom love clean quality code.

Until we meet again, happy coding.

Best regards,
Mike Nguyen