Share via


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.

Note

By default, code coverage is measured when you run unit tests. Therefore, when you run unit tests, you need to perform the steps in Obtaining Code Coverage Data only if code coverage has been turned off, or if you want to instrument additional artifacts in order to gather code coverage data for them.

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. For more information, see Re-Signing Assemblies.

You must explicitly instrument artifacts to obtain code coverage data when you run tests other than unit tests. For example, a tester who is running a manual test might start a particular program. If the program's binaries are instrumented, code coverage data will be gathered. For more information, see Manual Tests Overview.

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 How to: Create and Run a Unit Test.

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

    For more information, see How to: Specify a Test Run Configuration.

  3. Click Code Coverage.

  4. Under Select artifacts to instrument, select your solution's DLL, executable file, or directory. For example, if your solution is named ClassLibrary1, select the check box for the assembly named ClassLibrary1.dll whose path is <Solution Directory>\ClassLibrary1\bin\Debug.

    Note

    You can also select the DLL that contains your test project files. This generates code-coverage data for the methods in your test project, not just for the methods in your production code.

  5. Click Apply and then click Close.

  6. Run one or more tests.

    For more information, see How to: Run Selected Tests.

    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 in Test Edition.

    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.

Re-Signing Assemblies

To re-sign assemblies

  1. Open the test run configuration you will use for your unit tests.

    For more information, see How to: Specify a Test Run Configuration.

  2. Click Code Coverage.

  3. Click the ellipsis (…) next to the Re-signing key file text box.

    The Choose a Key File dialog box appears.

  4. Select a key file and then click Open.

  5. In the test run configuration editor, click Apply and then click Close.

    If you are testing multiple assemblies that have been signed, Visual Studio will try to re-sign all the strong-named assemblies that were signed with the key file that you specify. For more information, see "Re-sign the Assembly" in Instrumenting and Re-Signing Assemblies.

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. For more information, see How to: Specify a Test Run Configuration.

  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 Selected Tests

How to: Specify a Test Run Configuration

How to: Apply a Test Run Configuration

Troubleshooting in Test Edition

Concepts

How to: Create and Run a Unit Test

Instrumenting and Re-Signing Assemblies

Working with Merged Code Coverage Data