Code Coverage Data Overview

Code coverage data provides important insight into how effective your tests are at exercising your code. This data can be used to determine the amount of code that has been exercised by your tests and then to pinpoint the specific areas that were not exercised.

Code coverage data includes both statistics that are displayed in the Code Coverage Results window and highlights in the code itself. The following document lists and defines the code coverage statistics and in-source highlighting.

For information about how to obtain or view code coverage data, see How to: Obtain Code Coverage Data.

Code Blocks, Lines of Code, and Partial Lines

Code coverage data is calculated for code blocks, lines of code, and partial lines if they are executed by a test run. A code block is a code path with a single entry point, a single exit point, and a set of instructions that are all run in sequence. A code block ends when it reaches a decision point such as a new conditional statement block, a function call, exception throw, enter, leave, try, catch, or a finally construct.

For code coverage, a line of code is an executable line of code that excludes white space, comments, type declarations, and namespace declarations. A line of code can contain multiple code blocks. If a line of code contains multiple code blocks and only a portion of those code blocks were exercised by a test run, then it is calculated as a partial line.

Code Coverage Results Window

To determine whether you have achieved the desired amount of code exercised by your tests, view the Code Coverage Results window. The Code Coverage Results window displays statistics for the amount of code coverage in your project at the assembly, namespace, class, and method level. The data in the Code Coverage Results window shows the total number and percentage of lines of code and code blocks that were exercised by your tests.

For information about how to view the Code Coverage Results window, see How to: Obtain Code Coverage Data.

Code Coverage Software Measurements

The following list shows code coverage data that is displayed in the Code Coverage Results window:

Measurement

Description

Covered (Lines)

Displays the total number of lines of code that were exercised by your tests.

Not Covered (Lines)

Displays the total number of lines of code that were not exercised by your tests.

Covered (% Lines)

Displays the percentage of lines of code that were exercised by your tests.

Not Covered (% Lines)

Displays the percentage of lines of code that were not exercised by your tests.

Covered (Blocks)

Displays the total number of code blocks that were exercised by your tests.

Not Covered (Blocks)

Displays the total number of code blocks that were not exercised by your tests.

Covered (% Blocks)

Displays the percentage of code blocks that were exercised by your tests.

Not Covered (% Blocks)

Displays the percentage of code blocks that were not exercised by your tests.

Partially Covered (Lines)

Displays the total number of lines of code where some of the code blocks within the line were not executed.

Partially Covered (% Lines)

Displays the percentage of lines of code where some of the code blocks within the line were not executed.

Source Code Coverage Highlighting

To locate the specific code paths in your methods and determine whether they were exercised by a test run, view the code coverage highlighting within the source code. You can also use code coverage highlighting to determine whether a specific test was exercised during a test run in your unit test methods.

You can view code coverage highlighting for each method in your project. Code coverage highlighting provides three levels of highlighting that indicate the specific code blocks that were exercised during a test run. The highlighting can be viewed in all your methods, including unit tests methods. The total for each color of source highlighting is equal to the corresponding statistic in the Code Coverage Results window. For example, the statistic in the Code Coverage Results window for Lines Partially Covered should be equal to the number of beige highlights in the source code.

Color Coding for Source Code Coverage Highlighting

The following list shows the color coding for the code coverage highlighting that is displayed in the source code:

Color Coding

Description

Light Blue

Indicates that the entire line of code was exercised in the test run.

Beige

Indicates that only a portion of the code blocks within the line of code were exercised in the test run.

Reddish Brown

Indicates that the line was not exercised in the test run.

See Also

Tasks

How to: Obtain Code Coverage Data

How to: Merge Code Coverage Data

Walkthrough: Run Tests and View Code Coverage

Concepts

Working with Merged Code Coverage Data