Microsoft & .NET.NETCode Coverage for .NET applications: An Insight

Code Coverage for .NET applications: An Insight content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction to Code Coverage

In this article I will provide you with some insights about running code coverage on .NET framework applications. I will be creating the code coverage report by running code coverage on a sample piece of code. To perform the coverage of the .NET framework application code, Visual Studio 2010 tools will be used.

A Preface to Code Coverage

The technique of code coverage will enable the developers to make sure that their unit test has covered each and every piece of code that they have written. In other words we can say that code coverage helps the developer in making sure that whatever code they are releasing for quality assurance is unit tested and none of the untested code is released. Code coverage can be done by running either automated unit tests or by performing manual unit testing.

Note that the quality of code covered is always directly proportional to the quantity of code that a developer’s unit test covers. So code coverage plays a vital role in improving the quality of code that comes from a developer.

Visual Studio Tools for Running Code Coverage

Below are the steps involved in successfully executing the code coverage on .NET framework applications:

  1. Instrument the binary files.
  2. Start monitoring the code coverage as the unit tests are being executed.
  3. Stop monitoring.

The Visual Studio utilities which are used in running code coverage of the .net framework applications are listed below:

  1. VSINSTR.EXE – Used to instrument the DLL
  2. VSPERFCMD.EXE – Used to monitor the coverage of the code while the unit tests are done and would update the coverage report file accordingly. It is also used to stop the monitoring process.

Note that you should have Visual Studio Team System versions and coming to 2010 you should have Visual Studio 2010 Ultimate or above.

Running Code Coverage on a Sample Code

In this section let us create a console application in .net and run the code coverage for it. Create a console application and name it as CodeCoverageSample.

Add the code below to the Program.cs file and build the application:

namespace CodeCoverageSample
    class Program
        static void Main(string[] args)

        private static void DemonstrateCodeCoverage(bool booleanValue)
            if (booleanValue)
                Console.WriteLine("Boolean value is passed a true!");
                Console.WriteLine("Boolean value is passed a false!");

Now open up Visual Studio 2010 command prompt, change the path to the bin directory and run the command shown below to instrument the binary:

vsinstr /coverage CodeCoverageSample.exe

You will receive a successfully instrumented message and if you notice in the bin directory on the file system, two files–CodeCoverageSample.exe.orig and CodeCoverageSample.instr.pdb–would have been created.

Now run the command below to monitor and create the coverage report file:

vsperfcmd /start:coverage /output:demoapplication.coverage

The monitoring would have started now. You could make sure by looking for a file named demoapplication.coverage that will be created in the bin directory. This file is the code coverage report file. Having set up the environment for code coverage, now you can go ahead and rum the .EXE file. Once the unit testing is complete, execute the command below to shut down the coverage monitoring.

vsperfcmd /shutdown

Coverage Report

You can open the .coverage report file in the Visual Studio 2010 IDE directly and Fig 1.0 below shows the coverage report demoapplication.coverage opened in Visual Studio 2010:

Figure 1

The coverage can be seen in % measure and the code can be viewed with different color patterns just by double clicking on the method name. If you notice in Fig 1.0 above, the covered areas are displayed in green and the untested portion is displayed in red.


I hope this tutorial has helped you learn about code coverage and how to perform code coverage on .NET framework applications using the Visual Studio tools. Please make use of the comments sections below to add your comments or to ask questions if you have any.

Happy Reading!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories