Bad test coverage results? No worries!
Tags: testing, dotnet, ndepend, codequality
Published: 2019-11-06 10:21:42

<p>Yesterday I stumbled upon an “interesting” bug in the code coverage tool. In case you’re generating it (and I <strong>strongly encourage</strong> you to), please make sure you’re satisfied with the results.</p>



<p>At work by policy we have to ensure at least 70% of test coverage. However in one of my projects I was getting way worse results, in some cases even 0%.</p>



<p>I’m pretty paranoid about testing, and when possible I tend to follow TDD along with the <a href="https://martinfowler.com/articles/practical-test-pyramid.html" target="_blank" rel="noreferrer noopener" aria-label="Test Pyramid (opens in a new tab)">Test Pyramid</a>.</p>



<p>Long story short, I found out that if you have long running tests (eg. single tests taking more than 10 seconds to complete) <strong>AND </strong>you’re using .NET Core, the <strong>dotnet test</strong> command might terminate its execution <strong>before</strong> the report files get written. This will lead to bad results, in some cases even 0% ( which happened to me).</p>



<p>But fear not! The solution is at hand and relatively easy to implement. Just follow the steps outlined here: <a href="https://github.com/tonerdo/coverlet/issues/573#issuecomment-536593477" target="_blank" rel="noreferrer noopener" aria-label="https://github.com/tonerdo/coverlet/issues/573#issuecomment-536593477 (opens in a new tab)">https://github.com/tonerdo/coverlet/issues/573#issuecomment-536593477</a></p>



<p>In my case I managed to move from a very sad 25% to a shiny 78%. In one case I even got 90%!</p>



<p>Interesting enough, generating the coverage report <a href="https://docs.microsoft.com/en-us/visualstudio/test/using-code-coverage-to-determine-how-much-code-is-being-tested?view=vs-2019">directly from Visual Studio Enterprise</a> leads to even better results. </p>



<p>For more details you can take a look here:</p>



<ul><li><a rel="noreferrer noopener" aria-label="https://github.com/tonerdo/coverlet/issues/575 (opens in a new tab)" href="https://github.com/tonerdo/coverlet/issues/575" target="_blank">https://github.com/tonerdo/coverlet/issues/575</a></li><li><a rel="noreferrer noopener" aria-label="https://github.com/tonerdo/coverlet#vstest-integration (opens in a new tab)" href="https://github.com/tonerdo/coverlet#vstest-integration" target="_blank">https://github.com/tonerdo/coverlet#vstest-integration</a></li><li><a href="https://github.com/tonerdo/coverlet/blob/master/Documentation/Troubleshooting.md#collectors-integration">https://github.com/tonerdo/coverlet/blob/master/Documentation/Troubleshooting.md#collectors-integration </a> </li></ul>



<p>Oh and don’t forget that if you have NDepend, you can import the coverage results and have a very pretty report. </p>



<figure class="wp-block-image"><img src="https://i2.wp.com/www.ndepend.com/Doc/Treemap/TreemapColor.png?zoom=1.25&amp;w=788&amp;ssl=1" alt="" data-recalc-dims="1" width="650" height="502" src-orig="https://i2.wp.com/www.ndepend.com/Doc/Treemap/TreemapColor.png?w=788&amp;ssl=1" scale="1.25"><figcaption>courtesy of NDepend</figcaption></figure>



<p>They also have <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://www.ndepend.com/docs/code-coverage" target="_blank">an excellent tutorial</a> on the website that will guide you through all the steps.</p>



<p></p>