Home » C# » Test » Code coverage with Visual Studio 2013

Code coverage with Visual Studio 2013

Microsoft Visual Studio has the ability to tell you what is covered by your unit test. Not all edition has this feature, but Premium and Ultimate do.

First of all, where is the code coverage tool in Visual Studio? It’s in the Test menu under Analyze Code Coverage.

TestCoverageMenu

Once done, the Code Coverage Result panel open. You can open this panel by going to Test>Windows>Code Coverage Result if you want to open it later.

This panel indicates the number of code block and the percentage covered by all code. As you can see, event the unit test project is in the statistic (red arrow). What is important is the blue arrow, the project tested. As you can see in the image below, every thing is unit tested (100%).
CodeCoverageResult

Another feature is to display directly into the code which line has been hit and which one has not been hit. Visual Studio and code coverage tool can highlight in green all code that has been reach by unit tests and in red if no test has executed the line. To enable this feature, click on the Code Coverage Result the icon with multiple block.
CodeCoverageButtonForHighlight

Once clicked, you will see the code changing.

CodeCoverageHighlight

I have entitled this post with Visual Studio 2013 but in fact, this is available since 2008 without much change. It’s not well known but very interesting if you are using unit testing.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.