Skip to content

Visual Studio Coverage Tools

Daniel Palme edited this page Jul 12, 2019 · 2 revisions

vstest.console.exe

By default vstest.console.exe creates a *.coverage file. To generate a coverage report with ReportGenerator the file has to be converted to *.xml format.

To get the *.coverage file you can use the following command:

"c:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "PATH_OF_YOUR_EXECUTABLE_OR_DLL" /InIsolation /EnableCodeCoverage

Now you have to create a simple tool that converts the *.coverage file to *.xml format.

Create a new command line project in Visual Studio and add a reference to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Coverage.Analysis.dll.

Add the following code (paths are hard coded here, but could be supplied as arguments):

using Microsoft.VisualStudio.Coverage.Analysis;

namespace CoverageConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            using (CoverageInfo info = CoverageInfo.CreateFromFile(
                "PATH_OF_YOUR_*.coverage_FILE", 
                new string[] { @"DIRECTORY_OF_YOUR_DLL_OR_EXE"}, 
                new string[] { }))
            {
                CoverageDS data = info.BuildDataSet();
                data.WriteXml("converted.coveragexml");
            }
        }
    }
}

Before executing your tool, you have to copy C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Coverage.Symbols.dll to the output directory of your project.

CodeCoverage.exe

CodeCoverage.exe is another coverage tool that comes with Visual Studio 2012/2013 (Premium and Ultimate). By default CodeCoverage.exe creates a *.coverage file. To generate a coverage report with ReportGenerator the file has to be converted to *.xml format.

To get the *.xml file you can use the following command:

"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" collect /output:DynamicCodeCoverage.coverage "PATH_OF_YOUR_EXECUTABLE_OR_DLL"
"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output:DynamicCodeCoverage.coveragexml DynamicCodeCoverage.coverage

CodeCoverage.exe is also part of this Microsoft.CodeCoverage NuGet package. To generate the XML file you can use the following command:

%UserProfile%\.nuget\packages\microsoft.codecoverage\XYZ\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:DynamicCodeCoverage.coveragexml DynamicCodeCoverage.coverage
Clone this wiki locally