Top 1% !
Pearl of Wisdom
Popularity: 133rd place

Published on:


How to programmatically convert the Visual Studio coverage file to coveragexml by command line tool in csharp

This code shows how to programmatically export and convert the coverage data ( .coverage) generated by Visual Studio into a readable xml file (.coveragexml).
The xml file can be used to generated a code coverage report, eg using the ReportGenerator tool.

The conversion is done using the CoverageInfo and CoverageDS objects and invoking the method WriteXml once the data has been parsed from coverage file.

What you need to do is:
1- Create a command line project in Visual Studio.
2- Add a reference to Microsoft.VisualStudio.Coverage.Analysis.dll (eg C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\).
3- Copy Microsoft.VisualStudio.Coverage.Symbols.dll to the output directory of your project. 

This programs takes 3 arguments:
-Args[0]: data coverage file name (eg data.coverage)
-Args[1]: DLL used to generated the coverage file (eg myProject.dll)
-Args[2]: coveragexml file name to be generated (eg converted.coveragexml)

For all args you can specify the full path name.

Original link:
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using Microsoft.VisualStudio.Coverage.Analysis; namespace CodeCoverageConverter { class Program { static void Main(string[] args) { if (args == null || args.Length != 3 || args[0] == null || args[1] == null || args[2] == null || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1]) || string.IsNullOrEmpty(args[2])) { help(); return; } string coverageFileName = args[0]; string dllFileName = args[1]; string coverageXmlFileName = args[2]; Console.WriteLine("args[0] = " + coverageFileName); Console.WriteLine("args[1] = " + dllFileName); Console.WriteLine("args[2] = " + coverageXmlFileName); using (CoverageInfo info = CoverageInfo.CreateFromFile(coverageFileName, new string[] { dllFileName }, new string[] { })) { CoverageDS data = info.BuildDataSet(); data.WriteXml(coverageXmlFileName); } } static private void help() { Console.WriteLine("Help!"); Console.Write(@" Arguments Args[0]: data coverage file name (data.coverage) Args[1]: DLL to use (myProject.dll) Args[2]: coveragexml file name to be generated (converted.coveragexml) "); } } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus