Reader Level:
ARTICLE

Introduction to Unit Testing Framework of VS 2008: Part IV

Posted by Sateesh Arveti Articles | Testing August 20, 2008
This article gives an introduction to Code Coverage in VS Team System 2008.
  • 0
  • 0
  • 10946
Download Files:
 

In previous article, we discussed about Test Project Configuration. In this article, we will look into Code Coverage an important feature of this framework. Most of us know about code coverage and its use. Before release of Visual Studio Team System 2008, we use NClover to measure code coverage. Here, I will give an introduction to code coverage followed by its implementation..

Code Coverage:

We use to write lot of test cases to make our application stable. But, we don't know how much part of that code is covered in our test cases. Inorder to measure that, we use automated code coverage tools like NClover in .NET, Clover in Java and CoverageMeter for C++.

Open sample code attached than goto Class1.cs, update the definition of AddNumbers with this:

public int AddNumbers(int a1, int b1)

{

          int sum =a1+b1;

          if (sum < 10)

          {

                   return a1 + b1+10;

          }

          else if (sum < 20)

          {

                   return a1 + b1+20;

          }

          else

          {

                   return a1 + b1;

          }

}

 

Test Method for this in Class1Test.cs is:

 

public void AddNumbersTest()

{

          Class1 target = new Class1();

          int a1 = 5;

          int b1 = 15;

          int expected = 20;

          int actual;

          actual = target.AddNumbers(a1, b1);

          Assert.AreEqual(expected, actual);

}

 

Now, we have to measure how much part of our code in AddNumbers is covered by our test case AddNumbersTest. This is called measuring of branches Covered. For our inputs a1=5 and b1 =15, this block of code will be executed:

 

int sum =a1+b1;

else

{

 return a1 + b1;

}

Our test case will cover only one block (else), remaining two blocks is not covered by the test case. Double Click on LocalTestRun.testrunconfig and goto Code Coverage tab, check TargetApplication.dll and click Apply & Close. Goto Test --> Run --> All Tests in Solution. Than, open Code Coverage window (Test --> Windows --> Code Coverage Results) and the result will be like this:


If we double click on a method in Code Coverage Results window, it will show lines covered by test case in light blue and not covered in light brown color as shown below:


Like this way, we can see how much part of our code is covered by the test cases.

We can export the results to an xml file by clicking Export Results button in Code Coverage Results window.

In coming article, I will discuss other important features of this Framework. I am attaching code for reference. I hope this code will be useful for all.

COMMENT USING

Trending up