Export (0) Print
Expand All
Expand Minimize

Advanced Code Coverage Techniques with Visual Studio Team System

Visual Studio 2008

When you understand the fundamentals of gathering code coverage data, you can then experiment with customizing how code coverage data is obtained and displayed. In the article Code Coverage Basics with Visual Studio Team System, you learned about the type of code coverage analysis that Visual Studio Team System uses, and also how to gather code coverage data from in the IDE and from the command line. This article focuses on how to use the instrumentation and collection functions found in Microsoft.VisualStudio.Coverage.Monitor.dll and Microsoft.VisualStudio.Coverage.Analysis.dll to build code coverage functionality into your applications.

Code coverage features are available in Visual Studio Team System 2008 Development Edition, Visual Studio Team System 2008 Test Edition, and Visual Studio Team System 2008 Team Suite.

Dd347161.note(en-us,VS.90).gifNote:
This article is based in part on the TechNote TN_1214: Gathering code coverage information from the command line by Ian Huff.

Microsoft.VisualStudio.Coverage.Monitor.dll collects code coverage information. You could use the functions in Microsoft.VisualStudio.Coverage.Monitor.dll to write code to perform the same tasks as running vsinstr.exe and vsperfmon.exe from the command line.

For example:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.VisualStudio.CodeCoverage;
 
//TODO: Add a reference to Microsoft.VisualStudio.Coverage.Monitor.dll
 
namespace CoverControl
{
     class CoverProgram
     {
            static void Main(string[] args)
            {
                // TODO: Write code to call vsinstr.exe 
                Process p = new Process();
                StringBuilder sb = new StringBuilder("/COVERAGE ");
                sb.Append("myassembly.exe");
                p.StartInfo.FileName = "vsinstr.exe";
                p.StartInfo.Arguments = sb.ToString();
                p.Start();
                p.WaitForExit();
                // TODO: Look at the return code – 0 for success
 
                // A guid is used to keep track of the run
                Guid myrunguid = Guid.NewGuid();
                Monitor m = new Monitor();
                m.StartRunCoverage(myrunguid, "mytestrun.coverage");
 
                // TODO: Launch tests that can
                // exercise myassembly.exe
 
                // Complete the run
                m.FinishRunCoverage(myrunguid);
            }
     }
}

Microsoft.VisualStudio.Coverage.Analysis.dll extracts the data in the *.coverage file so that you can display the data in a useful format, such as an .XML file. You can also export code coverage information as an .XML file in the IDE. For more information, see How to: Export Profiling Tools Reports.

After you have a .coverage file, you could use the functions in Microsoft.VisualStudio.Coverage.Analysis.dll to convert the data to an XML format. For example:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.CodeCoverage;
 
//TODO: Add a reference to Microsoft.VisualStudio.Coverage.Analysis.dll
 
namespace CoverDump
{
       class DumpProgram
       {
              static void Main(string[] args)
              {
                     // Create a coverage info object from the file
                     CoverageInfo ci = CoverageInfoManager.CreateInfoFromFile("myfile.coverage");
 
                     // Ask for the DataSet
             // The parameter must be null
                     CoverageDS data = ci.BuildDataSet(null);
 
                     // Write to XML
                     data.Lines.WriteXml("mylines.xml");
              }
       }
}

This article has provided some examples of how you can use the instrumentation and collection functions found in Microsoft.VisualStudio.Coverage.Monitor.dll and Microsoft.VisualStudio.Coverage.Analysis.dll to build code coverage functionality into your applications. If you have additional questions about how to use these code coverage features, feel free to post them on the MSDN Forum Visual Studio Code Analysis and Code Metrics.

Community Additions

ADD
Show:
© 2014 Microsoft