How to get unit-testing code-coverage run from command line?

For efficiency & quickness, I prefer to run unit test & get the code coverage from the command line – this helps me spare the Visual Studio IDE to do other tasks. For me, running testing related tasks in the IDE is very slow, especially if you are working in a big project.

Here’s how to do it

1) Launch VS command prompt or change to the VS location which contains the tools, e.g. %ProgramFiles%\Microsoft Visual Studio 10.0\Team Tools\Performance Tools.

2) Instrument the product dlls using
vsinstr /coverage “Path\To\You\TestedAssembly.dll”

3) Start coverage monitor by running the command
vsperfcmd /start:coverage /output:”Path\Of\Report.code-coverage”

4) Run your tests which are testing the functionality of Path\To\You\TestedAssembly.dll
(manual or automated, doesnt matter)

5) Stop the coverage monitor by running the command
vsperfcmd /shutdown

Here’s the details

The first main step to collecting code coverage information is to instrument the assemblies that you are interested it. Instrumentation will insert code into the assemblies so that when they are run, code coverage information will be collected. You can instrument both managed and native dll files, exe files and assemblies. The tool that you use to instrument is called VsInstr.exe and it is found in the following directory

Microsoft Visual Studio 8\Team Tools\Performance Tools

Instrumenting the assembly is done by passing the –coverage option to the VsInstr.exe tool:

vsinstr –coverage MyAssembly.exe

When you instrument the assembly you are modifying it permanently, so the VsInstr tool automatically makes a backup of the file you instrument. In this case the backup will be called MyAssembly.orig.exe.

The next step in collecting code coverage information is to make sure that the collection monitor is running so that our collected coverage data gets written somewhere. The coverage monitor is called VsPerfMon.exe and is located in the same location as VsInstr.exe was located. To start it up for code coverage collection just use the following command:

start vsperfmon –coverage –output:mytestrun.coverage

Now the command shell will be running your monitor, waiting for some data to collect and write to the output file. Now is the time where you run your test suites or exercise your instrumented code in whatever manner you want to collect coverage information for. The command window will wait until the instrumented code has exited before it will close down and create the coverage file. Once it has, you will now have a code coverage file called mytestrun.coverage that details what code you exercised in your instrumented assembly while the monitor was running. You can open this file directly from Visual Studio to see a code coverage results window that will give you a breakdown of what percent of the code in your instrumented assembly was covered.


  1. (the idea)
    TN_1214: Gathering code coverage information from the command line
  2. (the details)
    Code coverage of generic unit testing

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: