Geeks With Blogs

Things Mark Flory Forgets Because Who Needs Memory When There is the Internet

I needed to be able to execute automated unit tests essentially command line for several different projects I am involved in.  That is easy enough to do with mstest.exe (I am using FinalBuilder so it is even easier than that).

What is not so easy is to gather the corresponding coverage statistics in something useful (i.e. XML).  I need to do that because I use a tool called NDepend to analyze our code base and it only understands the XML coverage file.

It took me a while to get it all figured out so I thought I would blog about the trail of Googling it took to get the info I needed.

First, this article explains how to get mstest setup to create your coverage data and get your code instrumented.  In there is a comment that led me to this MSFT forum post which ultimately led me to Joc's Blog and an article about creating a program to convert the coverage data to XML. 

That is what you need to do but he has a couple of things that won't work (probably because he was working off a Beta of VS 2005).


should just be


And also you need to set CoverageInfoManager.ExePath and CoverageInfoManager.SymPath to the path of the instrumented code.  If you don't set it or set it to the right path you will get this exception:

Error when creating coverage info: Error loading symbol file. Symbol and binary files should be in the same folder as the coverage file or on the symbol path

My code looks like this:

        static void Main(string[] args)


            String coveragepath = System.IO.Path.GetDirectoryName(args[0]);

            CoverageInfoManager.SymPath = coveragepath;

            CoverageInfoManager.ExePath = coveragepath;


            // Create a coverage info object from the file

            String coveragefile = System.IO.Path.GetFullPath(args[1]);

            CoverageInfo ci = CoverageInfoManager.CreateInfoFromFile(coveragefile);




            // Ask for the DataSet.  The parameter must be null

            CoverageDS data = ci.BuildDataSet(null);




            // Write to XML

            String coverageoutput = System.IO.Path.GetFullPath(args[2]);




I was intially confused on what to set those paths to so I set it to where the code was compiled to (/bin).  Eventually I figured out when you set up coverage on code it creates an instrumented version of your code and puts it in a directory off of your test directory.  One directory is called in, and that is where you will find your coverage file, the other is called Out, and that is where you will find your instrumented code.

In case you are not sure how to get coverage instrumented into your code you need to check out this.

Posted on Thursday, September 4, 2008 10:19 AM Metrics , Code Analysis , Security | Back to top

Comments on this post: Generating VSTS 2008 Coverage statistics

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Mark Flory | Powered by: