Code Coverage                                                                                
•        Code Coverage tells the developer what percentage of the source is unit tested.
-        By default Code Coverage is not turned on. You turn it on through the test configuration properties
window.
-        Below, I’ve gone ahead and checked the MathLib.dll project to find out the level of unit testing that is
being performed.

•        Looking at the Code Coverage results on the next page, you can see that there is a problem with the
Divide() method.
-        Apparently, it is not testing all the code within the entire method.
-        25% of that method is not being unit tested.

•        One of the cool things we can do with Code Coverage is use Code Coverage Coloring.  Let’s turn it on
by clicking the icon at the top of the Code Coverage window.

•        With the coloring enabled, we will be able to see what portions of the code are not being unit tested. Let’
s double-click the Divide method right here in the Code Coverage window.

•        As you can see the code that is red is never tested because our default test code always passes in zero
as the second parameter.
•        Let’s fix DivideTest() to call Divide twice while passing in 10 and 0 as y.
<TestMethod()> _
Public Sub DivideTest()
Dim target As Compute = New Compute

Dim x As Integer = 100
Dim y As Integer = 10
Dim expected As Integer = 10
Dim actual As Integer = 0

actual = target.Divide(x, y)
Assert.AreEqual(expected, actual, _
"MathLib.Compute.Divide did not return the expected value.")

x = 100 : y = 0 : expected = 0
actual = target.Divide(x, y)
Assert.AreEqual(expected, actual, _
"MathLib.Compute.Divide did not return the expected value.")
'Assert.Inconclusive("Verify the correctness of this test method.")
End Sub




•        As you can see, we now have 100% coverage on the four methods within the Compute class.
-        Why don’t we have 100% for the entire MathLib.dll?
-        We’re not testing the My and My.Resources namespaces that are automatically included with Visual
Basic projects.
•        Code Coverage percentages can be enforced within Team Foundation Server to ensure developers are
unit testing their code.
-        For example, a check-in policy may be created that all code checked in must have 75% Code Coverage.
We’ll cover this later.
Code Coverage
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials