One of the great new features in Visual Studio 2005 Team Suite, Team Edition for Developers or Team Edition for Testers is the unit testing and code coverage analysis. One limitation however is that the code coverage results are output to a binary file making it useless for reporting in 3rd party tools. You can export the results to XML from Visual Studio but I needed an easy way to automate the conversion to XML as part of an automated build process using CruiseControl.NET. Having the data in XML makes displaying the results in the CruiseControl.NET dashboard a piece of cake.
After doing a bit of research of I came across this MSDN article that gave me the solution I was after. The trick is to make use of the CoverageInfo class contained in the Microsoft.VisualStudio.Coverage.Analysis.dll assembly to convert the coverage data file to a Dataset which is then easily convertible to XML.
//set symbols/exe paths
CoverageInfoManager.SymPath = SymbolsPath;
CoverageInfoManager.ExePath = ExePath;
//load the specified code coverage data file
CoverageInfo cinfo = CoverageInfoManager.CreateInfoFromFile(CoverageDataFile);
//Generate a dataset from the code coverage data file
CoverageDS cdata = cinfo.BuildDataSet(null);
From the code sample above you can see that the first thing we need to do is specify the location of the symbol file and exe file. The reason this is necessary is because the coverage file is generated in a subfolder different than the binary and symbol file which are required for conversion. If these two paths aren't set you'll see and error such as this:
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:
Next we create a CoverageInfo object from our coverage data file, convert it to a CoverageDS Dataset and finally save it to disk as XML.
Complete source code for the console application I created can be found in the attachment below. Note that to compile the project you will need either Visual Studio 2005 Team Suite, Team Edition for Developers or Team Edition for Testers installed. The Microsoft.VisualStudio.Coverage.Analysis.dll assembly referenced by the project is typically located in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies.
CodeCoverageConverter.zip (26.66 kb)