Load Code Coverage Data to SQL database

Load Code Coverage Data to SQL database

Use Visual Studio’s code coverage tools to obtain the .coverage file

Visual Studio has a set of tools that can collect your code coverage data. The data structure of the collected code coverage data is in a table format. As you can see from the link, it shows how to obtain code coverage data, which is not what I am covering here. This article is talking about how to load the .coverage file into a SQL server database. You can refer to my previous blog about how to obtain x64 code coverage data. The result of the code coverage data is a .coverage file. Open it in Visual Studio 2008 Code Coverage Results panel (see attachments). When you expand the first + sign, Visual Studio reads the .pdb and binary files from where you originally executed the code coverage profiler. For example, suppose you collected code coverage data on a remote server and copied the .coverage file to your local machine. When you click on the + sign of the .coverage file, you will get “Code Coverage Analysis engine threw exception(s): 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”. The solution is copy the symbol (.pdb) files and the binary from the remote server to your local machine at the same path. Click on Export Results button in the panel to save to a .xml file. We will cover how to use the xml file in the later section.

Use code coverage API to retrieve code coverage data

The code coverage API is not documented. The closest thing you can get is joc's bLog. You could find the code coverage assembly at \Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Coverage.Analysis.dll. If you run in x64 Windows, make sure you configure your project properties to Platform Target: x86. Otherwise, you will get {"Could not load file or assembly 'Microsoft.VisualStudio.Coverage.Analysis, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. An attempt was made to load a program with an incorrect format."} exception. Microsoft.VisualStudio.Coverage.Analysis.dll is a 32 bit dll. joc's bLog shows how to use a query to filter what methods you want for method code coverage data:
using Microsoft.VisualStudio.CodeCoverage;
CoverageDSPriv.MethodDataTable methodDT = myCovDS.Method;
DataRow[] methodDR = methodDT.Select("MethodName IN " + searchmethods);
Above is the code breakdown. You can use “SqlBulkCopy” class to load “methodDT” to a SQL server table. You need to have the same column name in the table on the SQL server as those in “methodDT”. Below is the code that uses “SqlBulkCopy” class to import the code coverage data:
using (SqlBulkCopy SqlBCP = new SqlBulkCopy(connString))

{

    SqlBCP.DestinationTableName = "Method";

    SqlBCP.WriteToServer(methodDT);

}

Use SQL Server Integration Service project to create code coverage data tables

“SqlBulkCopy” class does not create the table structure for you. You need to create it manually. Although it’s only 8-9 columns depending on the CoverageDSPriv’s DataTable structure, there is actually a smarter way to create the table by using a SSIS (SQL server integration service) project through SQL Server Business Intelligence Development Studio. Make sure you have the following features installed in your SQL Server 2008: Business Intelligence Development Studio and Integration Services. Integration Services can run under Network Service account. Open Visual Studio 2008. Click on File > New > Project… Find Project types Business Intelligence Projects > Integration Services Project. Open the SSIS Packages > Package.dtsx, go to the Data Flow tab. Click to add a data flow task. Open Toolbox. Drop and drop XML Source into the designer surface. Double click on the XML source. Locate the XML file generated from the Code Coverage Results panel’s export results. Check “Use inline schema” and hit OK. Choose OK to accept the warnings. Drag and drop an OLE DB Destination to the designer surface. Highlight the XML source and point the green arrow to the OLE DB Destination. Select Output as what kind of CoverageDSPriv’s DataTable you want. For example, you can choose Class and hit OK. Double click on the OLE DB Destination. Click on New… at “OLE DB connection manager” to create a new connection to the database you want to load code coverage data to. Click on New at “Name of the table or the view” to see the SQL query that will create the table. Change the table name and hit OK. The table is now created. The OK button in the editor is disabled. Click on Mappings on the left pane and make sure the Input columns in CoverageDSPriv’s DataTable correctly map to the destination columns. Now, you can hit OK in the editor. Add more OLE DB Destination to the designer surface and link the XML source to it with different Output. If you see any error in the Error List, try to resolve them by double clicking on them to analyze. Removing incorrectly mapped references might help. When you see no errors, save the .dtsx file and open it in Execute Package Utility by double clicking on it. This should guide you through loading data to the SQL server. At this point, I always get errors when loading method coverage data to the database. There seems to be a truncation error. You might have to load it row by row in ADO.NET or LINQ by following the code sample in the previous section.

CodeCoveragePanel.png