How to: Obtain Code Coverage Data

You can measure the effectiveness of tests on a line-by-line or even a block-by-block basis. You do this by configuring test runs to produce code coverage data. The resulting data is displayed in the Code Coverage Results window and in your source code files.

Code coverage data is gathered when artifacts, typically binary files, have been instrumented and, during a test run, are loaded into memory. The Obtaining Code Coverage Data procedure describes how to select a file for instrumentation.

After the test run has completed, you can view code coverage data; for more information, see Viewing Code Coverage Data.

You can also merge multiple sets of code-coverage data, as described in How to: Merge Code Coverage Data. For information about various circumstances related to merging code coverage data, see Working with Merged Code Coverage Data.

As explained in Instrumenting and Re-Signing Assemblies, strong-named assemblies that have been instrumented must be re-signed. You enable re-signing by specifying a key file.

Obtaining Code Coverage Data

To obtain code coverage data

  1. Create tests for your code. These can be unit tests, or other test types that exercise code for which you have symbols, and you have selected the appropriate binaries to instrument.

    For information about how to create unit tests, see Creating and Running Unit Tests for Existing Code.

  2. Open the test settings you will use for your unit tests.

    For more information, see Create Test Settings to Run Automated Tests from Visual Studio.

  3. Follow the steps in this topic to configure the code coverage data that you want to collect: How to: Configure Code Coverage Using Test Settings for Automated Tests.

  4. Run one or more tests.

    For more information, see How to: Run Automated Tests from Microsoft Visual Studio.

    While the tests are running, code coverage data is gathered. For information about viewing it, see Viewing Code Coverage Data.

    Note

    Running VSPerfMon.exe can interact with the collection of code coverage data. For more information, see the section "Code Coverage Data and VSPerfMon.exe" in Troubleshooting for Testing Tools for Visual Studio ALM.

    Code coverage data cannot be collected for an application that is running in a 64-bit process. Therefore, when you have requested code coverage data while testing such an application, the test engine sets the "32BIT" flag in the Portable Executable (PE) header of the assembly to be instrumented. After the test run has completed, the assembly is returned to its original state.

Viewing Code Coverage Data

Prerequisite: You have run tests that have produced code-coverage data, as described in Obtaining Code Coverage Data.

To view code coverage data

  1. On the Test Results toolbar, click Code Coverage Results. Alternatively, click Windows on the Test menu, and then click Code Coverage Results.

    The Code Coverage Results window opens.

  2. In the Code Coverage Results window, the Hierarchy column displays one node that contains all the code-coverage data obtained in the latest test run. If an error occurred, the error message is displayed at this location, instead of the root node. If the node is displayed, expand it.

    Note

    By default, this test run node is named using the format <user name>@<computer name> <date> <time>. You can change the default naming scheme on the General page of the Options dialog box.

  3. Expand the node for the assembly, for the namespace, and for a class in your production code.

    The rows within the class represent its methods. The columns in this table display coverage statistics for individual methods, for classes, and for the entire namespace.

  4. Double-click the row for one of the methods in the class.

    The source-code file opens to the method you chose. In this file, you can see code highlighting. By scrolling, you can see the coverage for the other methods in this file. To change the colors in which lines of code are highlighted, see Changing the Display of Code Coverage Data.

    Note

    You can click buttons on the Test Tools toolbar to toggle the display of code coverage within the file and to navigate to the previous or next lines of code in the file.

  5. (Optional) If you selected the check box for your test project's DLL, you can open the source-code file that contains your unit tests to see which test methods were exercised.

Displaying Different Code Coverage Calculations

Code Coverage calculations display the Code Coverage totals as columns in the Code Coverage Results window. Up to eight columns can be enabled to display calculations for code that was covered by test runs as well as for code that was not covered. You can enable columns that display the calculations as a percentage or a numeric total. You can also enable columns that display calculations for lines of code and for code blocks.

To display or hide a Code Coverage data column

  1. Right-click any of the data in the Code Coverage Results window.

  2. Click Add/Remove Columns.

  3. Select the check boxes next to the items that you want to display and clear the check boxes next to the items that you want to remove.

  4. Click OK.

Changing the Display of Code Coverage Highlighting

By default, particular colors are used to indicate whether code was covered by tests that have been run. Lines that are highlighted light blue were exercised in the test run, and lines that are highlighted reddish brown were not. Within lines that are highlighted beige, some of the code was exercised and some was not.

To change the display of code coverage data

  1. Click Tools and then click Options.

    The Options dialog box is displayed.

  2. Expand Environment.

  3. Click Fonts and Colors.

  4. Under Show settings for, select Text Editor.

  5. Under Display items, select the area of code coverage whose display color you want to change. Your choices are Coverage Not Touched Area, Coverage Partially Touched Area, and Coverage Touched Area.

  6. Change the settings for this area of code coverage. You can change the foreground and background colors, and the font, font size, and bolding of the text.

  7. (Optional) Change the settings for other areas of code coverage.

  8. When you are finished, click OK.

See Also

Tasks

How to: Run Automated Tests from Microsoft Visual Studio

Create Test Settings to Run Automated Tests from Visual Studio

Troubleshooting for Testing Tools for Visual Studio ALM

Concepts

Creating and Running Unit Tests for Existing Code

Instrumenting and Re-Signing Assemblies

Working with Merged Code Coverage Data