Introduction to Unit Testing Framework of VS 2008

Unit Testing is common activity in our software development. Before release of Visual Studio 2008, most of us prefer to use NUnit for writing unit test cases.

NUnit Framework provides lot of features that will make writing test cases easier.

But, still it is having some drawbacks in it. Here, I will list some of the drawbacks of NUnit Framework followed by an introduction to new built-in testing framework of VS 2008.

Drawbacks of NUnit Framework:

  • Installation of NUnit comes in a separate MSI.
  • No Integration with Visual Studio.
  • Requires writing test cases manually. 
  • No auto generation of code.
  • Requires opening separate window (NUnit Console or GUI) to execute test cases.
  • Ordering of test cases execution not available.
  • No inbuilt feature to debug test cases.
  • No inbuilt feature to enable/disable test cases.
  • No inbuilt feature to give additional information of test cases like stack trace, Trace Information etc.
  • No inbuilt feature to sort test cases based on Computer Name, Class Name and Host Type etc.

Now, let's drill into new unit testing framework of VS 2008. Open your VS 2008, Create a New Project:

Project Type: Class Library
Project Name: TargetApplication

Next, add some sample methods to be tested. For simplicity sake, I am adding simple methods to Class1:

            public int Square(int a1)

                   {

                             return a1 * a1;

                   }

                   public int AddNumbers(int a1, int b1)

                   {

                             return a1 + b1;

                   }

So,we are going to test this methods by using Testing Framework of VS 2008. Now,right click on solution and select Add New Project with

Project Type : Test Project present in tree node Test.
Project Name : TargetApplicationTest.

We are ready with a Test Project.Than, right click on Test Project and select Add New Test.It will show a dialog box to select template for the test cases.As a beginner, we will use Unit Test Wizard and click Ok.

It will popup a selection window,where we can add Assembly or class for which test cases should be generated.Check TargetApplication.Class1 and click Ok to generate test cases automatically for all methods present in Class1.


Now, you can see Class1Test.cs in Test Project.In that,it already added all test methods.It will set default values to parameters.We can change those values and test.After removing unwanted code (Assert.Inconclusive stmt and comments) and changing default values, the test methods will be like this:

 

            [TestMethod()]

                   public void SquareTest()

                   {

                             Class1 target = new Class1();

                             int a1 = 5;

                             int expected = 25;

                             int actual;

                             actual = target.Square(a1);

                             Assert.AreEqual(expected, actual);

                   }

 

            [TestMethod()]

                   public void AddNumbersTest()

                   {

                             Class1 target = new Class1();

                             int a1 = 10;

                             int b1 = 15;

                             int expected = 25;

                             int actual;

                             actual = target.AddNumbers(a1, b1);

                             Assert.AreEqual(expected, actual);

                   }

We are ready with test cases.In order to run those, goto Test menu --> Run --> All Tests in Solution or use shortcut Ctrl+R,A.

Now,it will show result of test cases execution in Test Results window.If you are unable to see this window, just goto Test --> Windows --> click on Test Results.

In order to debug any test cases, just check that test case in Test Results Window.Keep a breakpoint in that test case.Now, click on Debug --> Debug Checked Tests in Test Results window.

We can group the test cases by selecting Group By combobox in Test Results window based on any criteria like Class Name,Duration etc.

We can see the history of test cases executed so far by selecting All in the combobox next to Run Details button.

In this article, I given a small introduction to VS 2008 Testing Framework.In coming articles, I will explain core features of this Framework.

I am attaching code for reference. I hope this code will be useful for all.


Similar Articles