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
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.
Open the test run configuration you will use for your unit tests.
For more information, see How to: Specify a Test Run Configuration.
Click Code Coverage.
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.
Click Apply and then click Close.
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
Open the test run configuration you will use for your unit tests.
For more information, see How to: Specify a Test Run Configuration.
Click Code Coverage.
Click the ellipsis (…) next to the Re-signing key file text box.
The Choose a Key File dialog box appears.
Select a key file and then click Open.
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
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.
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.
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.
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.
(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
Right-click any of the data in the Code Coverage Results window.
Click Add/Remove Columns.
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.
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
Click Tools and then click Options.
The Options dialog box is displayed.
Expand Environment.
Click Fonts and Colors.
Under Show settings for, select Text Editor.
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.
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.
(Optional) Change the settings for other areas of code coverage.
When you are finished, click OK.
See Also
Tasks
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